[英]Difference between NavigableSet, SortedSet and TreeSet in Java
TreeSet
puts an element in natural ordering or by the provided comparator . TreeSet
将元素放在自然顺序中或由提供的比较器放置。 SortedSet
is also keeps the element in natural order SortedSet
还使元素保持自然顺序 But what is the difference between them and NavigableSet?
但是它们和NavigableSet?
区别NavigableSet?
Where are NavigableSets
useful? NavigableSets
在哪里有用?
Some example to show its usage would be nice for beginners. 显示其用法的一些示例对于初学者来说会很好。
SortedSet is an interface (it defines the functionality) and Treeset is an implementation. SortedSet是一个接口(它定义了功能),Treeset是一个实现。 NavigableSet is also an interface subtype of the SortedSet. NavigableSet也是SortedSet的接口子类型。
You can't just write SortedSet<Integer> example = new SortedSet<Integer>();
你不能只写SortedSet<Integer> example = new SortedSet<Integer>();
You can however write SortedSet<Integer> example = new TreeSet<Integer>();
但是,您可以编写SortedSet<Integer> example = new TreeSet<Integer>();
As its name implies, NavigableSets are more useful for navigating through the set. 顾名思义,NavigableSets对于浏览集合更有用。
http://mrbool.com/overview-on-navigableset-subtype-of-java-collections/25417 offers a good tutorial on NavigableSets and some of the methods available when using one, that aren't available in a SortedSet. http://mrbool.com/overview-on-navigableset-subtype-of-java-collections/25417提供了一个关于NavigableSets的一个很好的教程以及使用它时可用的一些方法,这些方法在SortedSet中不可用。
I hope you will find useful the following excerpt from Java docs (see link to more details): 我希望您会从Java文档中找到以下摘录(请参阅更多详细信息的链接):
Methods lower , floor , ceiling , and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element. 方法较低 , 地板 , 天花板和较高的返回元素分别小于,小于或等于,大于或等于,并且大于给定元素。
我觉得this
是一个很好的参考,有很好的解释。
NavigableSet添加了导航方法,如descendingIterator()和descendingSet(),ceiling(),floor(),higher(),lower(),headSet(),tailSet(),subSet(),pollFirst()和pollLast()。
TreeSet实现了NavigableSet,而(interface)NavigableSet扩展了SortedSet
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.