1

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());
        }

2 Answers 2

2

You are adding a single element to values, which is the String representation of that Collection (the String "[3,6]" in your case) :

values.add(BulkValues.toString());

If you want to add all the elements, use :

values.addAll(BulkValues);

or just declare values as :

ArrayList <String> values = new ArrayList<>(BulkValues);
Sign up to request clarification or add additional context in comments.

7 Comments

Your explanation make sense, but i encountered an error applying it as you could see in my updates of main thread ? is it because the HashMap <String, String> snapshot, is a single extracted as a single String and cant be compared ?
you are too fast, check again LOL. Plus i've tried "venu" suggestion but i see in his code he did not consider the fact that i am receiving the data from firebase as a snapshot,which i guess it is an object, and i am not inputing the data in my own, so i tried to use his code, but i was not sure how to do so, and if i did it correctly ?
@JanusJanus The error you got implies that the value in the snapshot Map is Long, not String, so casting it to (HashMap<String,String>) is probably wrong. You should check the actual runtime types you are getting, and use the correct types in your code.
i hope you could patient with me hear, since i am not that familiar with several things, how could i check the runtime types ? and even when i tried to change all the " String " in the code to Long, at the end i get an red underline the "compare" in : textViewRatingValue.setText(Collections.max(values, compare));
@JanusJanus If values is a List<Long> you don't need the Comparator. Just use Collections.max(values).
|
1

Here you are adding total array as a string (values.add(BulkValues.toString())). instead of that you need to use addAll like this values.addAll(BulkValues);

See below example.

Map<String, String> map=new HashMap<>();
            map.put("one", "1");
            map.put("six", "6");
            map.put("three", "3");
            map.put("nine", "9");
            map.put("seven", "7");
             Map<String, String> ratings =map;
             Collection<String> BulkValues = ratings.values();
             ArrayList <String> values = new ArrayList<>();
             values.addAll(BulkValues);

             Comparator<String> compare = new Comparator<String>() {
                 @Override
                 public int compare(String lhs, String rhs) {
                     return Integer.valueOf(lhs).compareTo(Integer.valueOf(rhs));
                 }
             };

             System.out.println(Collections.max(values, compare));
            }

output:

9

Hope it will help you.

3 Comments

i've tried your suggestion but i am receiving the data from firebase as a snapshot,which i guess it is an object, so am not sure how pass the data, and if i did it correctly ?
@JanusJanes Sorry i didn't observe properly. Here i guess snapshot.getValue() returns Hashmap object with long values but you are assigning to Hashmap<String,String> this is wrong.. So you need to do like this (Hashmap<String,Long>) ratings.value();
Yes, Thank you @venu, worked now, and understood the concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.