java TreeSet - 不要删除重复项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5895448/
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
java TreeSet - don't remove duplicate items
提问by pengguang001
TreeSet removes different items with the same Comprator value. I don't want it be removed. Is there any way to control this? Or use another container class?
TreeSet 删除具有相同 Comprator 值的不同项目。我不希望它被删除。有没有办法控制这个?或者使用另一个容器类?
Added: OK. It seems I can't use Set. I need insert sorting feature, for performance consideration. Can List do this? Thanks all.
补充:好的。好像不能用Set。出于性能考虑,我需要插入排序功能。List能做到吗?谢谢大家。
采纳答案by Heiko Rupp
A set by definition can not have duplicate entries.
根据定义,集合不能有重复的条目。
So you need to use a List or Array or such
所以你需要使用 List 或 Array 之类的
回答by deepkimo
Even it is a set, this is still confusing because the objects are different. For example, a Set<E>
of different objects E
will drop some objects when converted to a TreeSet<E>
based on the Comparator<E>
used. In both cases, it is a set, but the set of elements stored will be different. In my opinion this is not clarified well in the docs.
即使是一个集合,这仍然令人困惑,因为对象不同。例如,Set<E>
不同的对象的E
a 转换为TreeSet<E>
基于Comparator<E>
使用的a 时会丢弃一些对象。在这两种情况下,它都是一个集合,但是存储的元素集合会有所不同。在我看来,这在文档中没有得到很好的澄清。
A simple solution, if you can change the Comparator, let it not return 0. For example instead of:
一个简单的解决方案,如果你可以改变Comparator,让它不返回0。例如代替:
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2);
}
Use:
利用:
public int compare(Integer o1, Integer o2) {
return o1 < o2 ? -1: 1;
}
回答by Alan Escreet
回答by Andrey Adamovich
回答by Denis Lukenich
If you want a SortedList you can for example take a list and manually call Collections.sort() after each insert.
如果你想要一个 SortedList 你可以例如获取一个列表并在每次插入后手动调用 Collections.sort() 。
Or you wrap e.g. an ArrayList to ensure the sort-calls for you:
或者您包装例如一个 ArrayList 以确保为您进行排序调用:
class SortedArrayList extends ArrayList<String> {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void add(int index, String element) {
super.add(index, element);
Collections.sort(this);
}
@Override
public boolean add(String element) {
boolean returnValue = super.add(element);
Collections.sort(this);
return returnValue;
}
@Override
public boolean addAll(Collection<? extends String> c) {
boolean returnValue = super.addAll(c);
Collections.sort(this);
return returnValue;
}
@Override
public boolean addAll(int index, Collection<? extends String> c) {
boolean returnValue = super.addAll(index, c);
Collections.sort(this);
return returnValue;
}
@Override
public String set(int index, String element) {
String returnValue = super.set(index, element);
Collections.sort(this);
return returnValue;
}
}
I hope I got all functions which can require sorting. (Remove is not necessary to override)
我希望我得到了所有需要排序的函数。(删除不需要覆盖)