【问题标题】:java ArrayList retrieving highest valuejava ArrayList检索最高值
【发布时间】:2016-07-26 12:41:42
【问题描述】:

执行此代码应输出数字 6,因为我只从 Data.snapshot [3,6] 中检索两个值。但我一直以这种形式获得它们:[3,6]。 是不是我做错了什么,或者我对提取的快照和 HashMap 的理解不正确,我怎样才能得到最高值?

     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) {

        }
    });
}

应用Erans的建议,代码是;

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

和错误:

 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

是不是因为它仍然被视为单个字符串 [3,6] 并且无法比较?还是因为我以错误的方式将它传递给 textView“textViewRatingValue”?

好吧,感谢Eran的精确解释,检索到的数据是“Long”,因此必须这样指出,这是我犯的第二个错误,这样做更简单,如下所示;

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

【问题讨论】:

    标签: java android-studio arraylist collections hashmap


    【解决方案1】:

    在这里,您将总数组添加为字符串(values.add(BulkValues.toString()))。而不是你需要像这样使用addAll values.addAll(BulkValues);

    见下面的例子。

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

    输出:

    9
    

    希望对你有帮助。

    【讨论】:

    • 我已经尝试过你的建议,但我从 firebase 接收数据作为快照,我猜它是一个对象,所以我不确定如何传递数据,如果我做得正确?
    • @JanusJanes 抱歉,我没有正确观察。在这里我猜 snapshot.getValue() 返回具有长值的 Hashmap 对象,但是您分配给 Hashmap 这是错误的..所以您需要这样做 (Hashmap) rating.value() ;
    • 是的,谢谢@venu,现在工作了,理解了这个概念。
    【解决方案2】:

    您正在向values 添加单个元素,这是CollectionString 表示(在您的情况下为String "[3,6]"):

    values.add(BulkValues.toString());
    

    如果要添加所有元素,请使用:

    values.addAll(BulkValues);
    

    或者只是将values 声明为:

    ArrayList <String> values = new ArrayList<>(BulkValues);
    

    【讨论】:

    • 您的解释是有道理的,但是我在应用它时遇到了错误,正如您在我的主线程更新中看到的那样?是不是因为 HashMap 快照,是单提取为单字符串,无法比较?
    • 你的速度太快了,再检查一遍哈哈。另外,我尝试过“venu”建议,但我在他的代码中看到他没有考虑我从 firebase 接收数据作为快照的事实,我猜这是一个对象,我没有在我的自己的,所以我尝试使用他的代码,但我不知道该怎么做,如果我做得正确?
    • @JanusJanus 您得到的错误意味着snapshot 映射中的值是长的,而不是字符串,因此将其转换为(HashMap&lt;String,String&gt;) 可能是错误的。你应该检查你得到的实际运行时类型,并在你的代码中使用正确的类型。
    • 我希望你能耐心听我说,因为我对一些事情不太熟悉,我如何检查运行时类型?甚至当我尝试将代码中的所有“字符串”更改为长时,最后我在“比较”中得到一个红色下划线:textViewRatingValue.setText(Collections.max(values, compare));
    • @JanusJanus 如果值是List&lt;Long&gt;,则不需要比较器。只需使用Collections.max(values)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2016-12-30
    • 1970-01-01
    相关资源
    最近更新 更多