java 为什么 Collections.max() 不返回字符串集合的实际最大值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15403183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:27:52  来源:igfitidea点击:

Why does Collections.max() not return actual max value for a Collection of String?

javacollectionscompare

提问by Bishan

ArrayList<String> dirNo = new ArrayList<String>();

dirNo.add("1");
dirNo.add("2");
dirNo.add("3");
dirNo.add("4");
dirNo.add("5");
dirNo.add("6");
dirNo.add("7");
dirNo.add("8");
dirNo.add("9");
dirNo.add("10");
dirNo.add("11");

System.out.println("max : " + Integer.parseInt(Collections.max(dirNo)));

After executing above code, print 9as output.

执行上述代码后,打印9作为输出。

But actually max value should be 11.

但实际上最大值应该是11

Why am I getting 9as max ?

为什么我的最大值为9

回答by NPE

Since your elements are strings, Collections.max()is returning the value that's the largest lexicographically.

由于您的元素是字符串,Collections.max()因此返回按字典顺序最大的值。

If you wish to compare the strings numerically, you need to use the two-argument version of Collections.max()and supply an appropriate comparator:

如果您希望以数字方式比较字符串,则需要使用 的双参数版本Collections.max()并提供适当的比较器:

    ArrayList<String> dirNo = new ArrayList<String>();

    dirNo.add("1");
    dirNo.add("2");
    dirNo.add("3");
    dirNo.add("4");
    dirNo.add("5");
    dirNo.add("6");
    dirNo.add("7");
    dirNo.add("8");
    dirNo.add("9");
    dirNo.add("10");
    dirNo.add("11");

    Comparator<String> cmp = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
        }
    };
    System.out.println("max : " + Collections.max(dirNo, cmp));

回答by Dariusz

You are using a String collection! String comparison is completely different from number comparsion.

您正在使用 String 集合!字符串比较与数字比较完全不同。

String value "2" > "11"because '2' > '1'(first character difference)

字符串值"2" > "11"因为'2' > '1'(第一个字符差异)

回答by Kumar Shashwat

Change String to Integer

将字符串更改为整数

ArrayList<Integer> dirNo = new ArrayList<Integer>();

That's it.

而已。

回答by Anshul Singh Panwar

this should work fine

这应该可以正常工作

String[] allLongestStrings(String[] inputArray) {
    ArrayList<String> newArray = new ArrayList<String>();
    ArrayList<Integer> arrayIndex = new ArrayList<Integer>();
    ArrayList<String> longestString = new ArrayList<String>();

    for(int i=0; i < inputArray.length;i++){
        newArray.add(inputArray[i]);
        arrayIndex.add(inputArray[i].length());
    }

    int maximumVal = Collections.max(arrayIndex);

    for(int j=0; j < newArray.size();j++){
        if(newArray.get(j).length() >= maximumVal){
            longestString.add(newArray.get(j));
        }    
    }   

    String[] stringArray = longestString.toArray(new 
    String[longestString.size()]);
    return stringArray;

}

}