java 使用 IgnoreCase 按键对 Map<String, Object> 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1691564/
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
Sort Map<String, Object> by keys with IgnoreCase?
提问by d0pe
Well, I tested TreeMap but it doesn't take in account IgnoreCase on string comparision. I need to order lexicographically and ignoring case. Is there any other way?
好吧,我测试了 TreeMap 但它没有考虑字符串比较的 IgnoreCase。我需要按字典顺序排序并忽略大小写。有没有其他办法?
Thanks, that works (TreeMap (Comparator c)). However, I have another question:
谢谢,有效(TreeMap(比较器c))。不过,我还有一个问题:
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
谢谢,马丁
回答by Michael Brewer-Davis
You want to use a Comparatorin the TreeMapconstructor. In particular, look at String.CASE_INSENSITIVE_ORDER.
您想Comparator在TreeMap构造函数中使用 a 。特别是,看看String.CASE_INSENSITIVE_ORDER。
TreeMap map = new TreeMap(String.CASE_INSENSITIVE_ORDER);
Using Collatoror a custom Comparatormay work better for you in a non-English locale or if you need more complex ordering.
在非英语语言环境中或者如果您需要更复杂的排序,使用Collator或 自定义Comparator可能更适合您。
回答by Chi
The best way would be to use a Collator. A collator is a built in class, which also implements Comparable, and therefore you can use it for your TreeMap.
最好的方法是使用 Collator。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 stringCollator = Collator.getInstance();
stringCollator.setStrength(Collator.PRIMARY);
new TreeMap<String, String>(stringCollator)
回答by Murali VP
Provide it a Comparator that compares strings case ignored.
为它提供一个 Comparator 来比较忽略大小写的字符串。
TreeMap(Comparator c)
TreeMap(比较器c)
回答by shoover
If you had a list, I would say try Collections.sort() with a Comparator. You may have to roll your own Comparator that uses String.equalsIgnoreCase() instead of equals().
如果你有一个列表,我会说用 Comparator 试试 Collections.sort()。您可能必须推出自己的使用 String.equalsIgnoreCase() 而不是 equals() 的比较器。
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)
回答by dustmachine
Yes, what you want is to use the TreeMap constructor that takes a Comparator. Make a comparator that uses compareToIgnoreCase.
是的,您想要的是使用带有 Comparator 的 TreeMap 构造函数。制作一个使用 compareToIgnoreCase 的比较器。
回答by Jeff Paquette
I suspect that you should construct your TreeMapwith 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);
}
}
回答by camickr
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。

