使用下面的Java代码是否可靠:
TreeSet<String> ts = new TreeSet<String>();
String stringAtIndexThree = Arrays.<Tag> asList(list.toArray(new Tag[list.size()])).get(3);
获取第三个索引处的对象(假设 ts.size() > 3
)?
也就是说,将 TreeSet<T>#toArray(T[])
如果没有对集合进行任何修改,总是以相同的顺序返回元素?
如果重要的话,这是针对 ComboBoxModel
的应该只包含唯一元素的实现(最好,我会使用不存在的 UniqueList
)。
谢谢!
厕所
最佳答案
will TreeSet#toArray(T[]) always return the elements in the same order, if no modifications are made to the set?
绝对 - TreeSet
以相同的排序顺序返回元素。当然,在实现可比较时,您的元素应该很好地发挥作用,以使排序顺序达到您的预期。
关于java - `TreeSet` 的可订购性可靠性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468461/