[英]Sort Map<String, Object> by keys with IgnoreCase?
Well, I tested TreeMap but it doesn't take in account IgnoreCase on string comparision. 好吧,我测试了TreeMap,但它没有考虑字符串比较中的IgnoreCase。 I need to order lexicographically and ignoring case. 我需要按字典顺序排序并忽略大小写。 Is there any other way? 还有其他方法吗?
Thanks, that works (TreeMap (Comparator c)). 谢谢,这是有效的(TreeMap(Comparator c))。 However, I have another question: 但是,我有另一个问题:
public final Comparator<Object> STR_IGN_CASE_COMP = new Comparator<Object>() {
public int compare(Object h1, Object h2) {
String s1 = h1.getId();
String s2 = h2.getId();
return s1.compareToIgnoreCase(s2);
}
}; //STR_IGN_CASE_COMP
How can I universialize the comparator to work with different objects? 如何将比较器广泛化以适应不同的对象? assuming all have the getId() method. 假设所有人都有getId()方法。
Thanks, Martin 谢谢,马丁
You want to use a Comparator
in the TreeMap
constructor . 您想在TreeMap
构造函数中使用Comparator
。 In particular, look at String.CASE_INSENSITIVE_ORDER
. 特别是,请查看String.CASE_INSENSITIVE_ORDER
。
TreeMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
Using Collator
or a custom Comparator
may work better for you in a non-English locale or if you need more complex ordering. 使用Collator
或自定义Comparator
可能会在非英语语言环境中更好地为您工作,或者如果您需要更复杂的排序。
The best way would be to use a Collator. 最好的方法是使用Collator。 A collator is a built in class, which also implements Comparable, and therefore you can use it for your TreeMap. collator是一个内置类,它也实现了Comparable,因此您可以将它用于TreeMap。
With a collator, you can also control the strength of the comparision, for example, if you want to be accent-insensitive as well. 使用collator,您还可以控制比较的强度,例如,如果您想要对重音不敏感。
Collator stringCollator = Collator.getInstance();
stringCollator.setStrength(Collator.PRIMARY);
new TreeMap<String, String>(stringCollator)
Provide it a Comparator that compares strings case ignored. 为它提供一个Comparator,用于比较被忽略的字符串。
TreeMap(Comparator c) TreeMap(比较器c)
If you had a list, I would say try Collections.sort() with a Comparator. 如果您有一个列表,我会说使用Comparator尝试Collections.sort()。 You may have to roll your own Comparator that uses String.equalsIgnoreCase() instead of equals(). 您可能必须使用String.equalsIgnoreCase()而不是equals()来滚动自己的Comparator。
public static <T> void sort(List<T> list,
Comparator<? super T> c)
But I was on the right track. 但我走在正确的轨道上。 Try the TreeMap constructor that takes a Comparator: 尝试使用Comparator的TreeMap构造函数:
public TreeMap(Comparator<? super K> comparator)
Yes, what you want is to use the TreeMap constructor that takes a Comparator. 是的,你想要的是使用带有比较器的TreeMap构造函数。 Make a comparator that uses compareToIgnoreCase. 制作一个使用compareToIgnoreCase的比较器。
I suspect that you should construct your TreeMap
with a custom comparator. 我怀疑你应该使用自定义比较器构建你的TreeMap
。
import java.text.Collator;
import java.util.Comparator;
class IgnoreCaseComp implements Comparator<String> {
Collator col;
IgnoreCaseComp() {
col = Collator.getInstance();
col.setStrength(Collator.PRIMARY);
}
public int compare(String strA, String strB) {
return col.compare(strA, strB);
}
}
How can I universialize the comparator to work with different objects? 如何将比较器广泛化以适应不同的对象? assuming all have the getId() method. 假设所有人都有getId()方法。
You should be able to use a BeanComparator . 您应该能够使用BeanComparator 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.