解释这个比较器?
Explain this comparator?
我知道 int maxLength
return 是列表中最长单词的长度,但我认为 return arg0.length() - arg1.length();
应该 return 类似于 -1
0
或 1
。那么这些值如何设置为 .length()
最后
int maxLength = Collections.max(lst, new Comparator<String>() { //get length of longest word using Collections.max comparator
@Override
public int compare(String arg0, String arg1) {
return arg0.length() - arg1.length();
}
}).length();
这个
arg0.length() - arg1.length()
将return表示arg0与arg1比较的整数,如果为0,则两者在此逻辑比较中相等,如果为-x则arg1比arg1大x,如果为x, arg0 是 x 大于 arg1
could you explain the .length() part as well? how is a 1 set to .length() at the end. – FatFockFrank just now
此语句类似于
String longestString = Collections.max(lst, new Comparator<String>() { //get length of longest word using Collections.max comparator
@Override
public int compare(String arg0, String arg1) {
return arg0.length() - arg1.length();
}
});
int maxLength = longestString.length();
return arg0.length() - arg1.length();
Returns 如果 arg0 更长则为正,如果它们长度相等则为 0,如果 arg1 更长则为负。返回值是正数还是负数(或零)无关紧要,因此超出该值的实际值将被忽略。例如,返回值 -50 被视为与 -1 相同。
Comparator 提供了一种比较两个元素的方法。
来自API:
public static T max(Collection coll, Comparator comp)
Returns the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
这会产生一个字符串(在 lst
中具有最长的字符串长度)并且 Collections.max(...).length()
将 return 它的长度。
我知道 int maxLength
return 是列表中最长单词的长度,但我认为 return arg0.length() - arg1.length();
应该 return 类似于 -1
0
或 1
。那么这些值如何设置为 .length()
最后
int maxLength = Collections.max(lst, new Comparator<String>() { //get length of longest word using Collections.max comparator
@Override
public int compare(String arg0, String arg1) {
return arg0.length() - arg1.length();
}
}).length();
这个
arg0.length() - arg1.length()
将return表示arg0与arg1比较的整数,如果为0,则两者在此逻辑比较中相等,如果为-x则arg1比arg1大x,如果为x, arg0 是 x 大于 arg1
could you explain the .length() part as well? how is a 1 set to .length() at the end. – FatFockFrank just now
此语句类似于
String longestString = Collections.max(lst, new Comparator<String>() { //get length of longest word using Collections.max comparator
@Override
public int compare(String arg0, String arg1) {
return arg0.length() - arg1.length();
}
});
int maxLength = longestString.length();
return arg0.length() - arg1.length();
Returns 如果 arg0 更长则为正,如果它们长度相等则为 0,如果 arg1 更长则为负。返回值是正数还是负数(或零)无关紧要,因此超出该值的实际值将被忽略。例如,返回值 -50 被视为与 -1 相同。
Comparator 提供了一种比较两个元素的方法。
来自API:
public static T max(Collection coll, Comparator comp)
Returns the maximum element of the given collection, according to the order induced by the specified comparator. All elements in the collection must be mutually comparable by the specified comparator (that is, comp.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the collection).
这会产生一个字符串(在 lst
中具有最长的字符串长度)并且 Collections.max(...).length()
将 return 它的长度。