我正在尝试找到一个实现java.util.List
and java.util.Set
同时在Java中。我希望这个类只允许唯一的元素(如Set
)并保留它们的顺序(例如List
)。 JDK 6 中存在吗?
重要的是拥有List<T>#add(int, T)
这样我就可以插入到特定的位置。
TreeSet http://download.oracle.com/javase/6/docs/api/java/util/TreeSet.html按元素顺序排序;LinkedHashSet http://download.oracle.com/javase/6/docs/api/java/util/LinkedHashSet.html保留插入顺序。希望其中之一就是您所追求的。
您已指定希望能够插入随意的位置,我怀疑你必须编写自己的 - 只需创建一个包含HashSet<T>
and an ArrayList<T>
;添加项目时,请先检查它是否在集合中,然后再将其添加到列表中。
或者 Apache 的 commons-collections4 提供ListOrderedSet https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/set/ListOrderedSet.html and SetUniqueList https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/list/SetUniqueList.html,其行为类似并且应该满足给定的要求。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)