简体   繁体   English

Java中的NavigableSet,SortedSet和TreeSet之间的区别

[英]Difference between NavigableSet, SortedSet and TreeSet in Java

  • A TreeSet puts an element in natural ordering or by the provided comparator . TreeSet将元素放在自然顺序中或由提供的比较器放置。
  • A 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.

相关问题 这些TreeSet声明之间的区别 - Difference between these declaration of TreeSet Java集合类型不匹配:无法从TreeSet转换为SortedSet - Java collection Type mismatch: cannot convert from TreeSet to SortedSet 不兼容的类型-SortedSet和TreeSet - Incompatible types - SortedSet and TreeSet .tailSet应该返回NavigableSet,但似乎返回TreeSet - .tailSet supposedly returns NavigableSet, but seems to return TreeSet 为什么在SortedSet中没有“ descendingIterator()”,而在TreeSet中呢? - Why there's no “descendingIterator ()” in SortedSet but it is in TreeSet? Java中不可修改的NavigableSet / NavigableMap? - Unmodifiable NavigableSet/NavigableMap in Java? 仅使用NavigableSet / SortedSet接口按首字母搜索字符串 - Search string by first letters using NavigableSet/SortedSet interfaces only 为什么NavigableSet的“布尔包含”版本返回NavigableSet,而没有“布尔包含”版本则返回SortedSet? - Why do the “boolean inclusive” versions of NavigableSet return a NavigableSet, and those without the “boolean inclusive” a SortedSet? TreeSet 的以下两个构造函数有什么区别? - What is the difference between the following two constructors of TreeSet? Hashset,Treeset和LinkedHashset,Hashmap之间的主要区别是什么?它在Java中如何工作? - What is the main difference between Hashset, Treeset and LinkedHashset, Hashmap and how does it work in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM