Executing this code should outcome number 6 since i am only retrieving two values from Data.snapshot [3,6]. but i keep getting both of them in this form : [3,6]. Is there something i am doing wrong or my understanding of the extracted snapshot and the HashMap is not correct, and how could i get the highest value ?
public void ratingCount(){
Firebase ref = new Firebase("https://CLOUD_NAME.firebaseio.com/rating/"+UserID);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Map<String, String> ratings = (HashMap<String,String>) snapshot.getValue();
Collection<String> BulkValues = ratings.values();
ArrayList <String> values = new ArrayList<>();
values.add(BulkValues.toString());
Comparator<String> compare = new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return Integer.valueOf(lhs).compareTo(Integer.valueOf(rhs));
}
};
textViewRatingValue.setText(Collections.max(values, compare));
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
Applying Erans suggestion, the code is ;
public void onDataChange(DataSnapshot snapshot) {
Map<String, String> ratings = (HashMap<String,String>) snapshot.getValue();
Collection<String> BulkValues = ratings.values();
ArrayList <String> values = new ArrayList<>(BulkValues);
Comparator<String> compare = new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return Integer.valueOf(lhs).compareTo(Integer.valueOf(rhs));
}
};
textViewRatingValue.setText(Collections.max(values, compare));
}
and the Error :
04-06 13:07:37.353 31832-31832/net.we4x4.we4x4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.we4x4.we4x4, PID: 31832
java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
at net.we4x4.we4x4.MyInformation$7$1.compare(MyInformation.java:259)
at java.util.Collections.max(Collections.java:1629)
at net.we4x4.we4x4.MyInformation$7.onDataChange
Is it because it is still being treated as one single String [3,6] and cant be compared ? or because i am passing it to textView " textViewRatingValue " in a wrong way ?
Well, Thanks to Eran precise explanation, the retrieved data is "Long" thus it must be indicated as so, that was the second mistake i was doing, and doing so getting the max is simpler as following;
Map<Long, Long> map = (HashMap<Long, Long>) snapshot.getValue();
Map<Long, Long> ratings = map;
Collection<Long> BulkValues = ratings.values();
ArrayList<Long> values = new ArrayList<>();
values.addAll(BulkValues);
Long max = Collections.max(values);
textViewRatingValue.setText(max.toString());
}