java Java中Set和HashSet的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35885258/
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
Difference between Set and HashSet in Java
提问by user3640657
What is the difference between Set
and Hash Set
in java?
When to use either of these?
java 中的Set
和 和有什么区别Hash Set
?何时使用其中任何一个?
回答by saroff
Set
is an interface, HashSet
- implementation of interface. It is recommended to use interface instead of implementation when you declaring variables.
Set
是一个接口,HashSet
- 接口的实现。建议在声明变量时使用接口而不是实现。
If go further into details, an interface in Java is a set of methods, and if some class wants to implement this interface, it must implement all of its methods.
如果再深入一点,Java 中的接口就是一组方法,如果某个类要实现这个接口,就必须实现它的所有方法。
The Set
interface represents a set of some objects, non-ordered, without random-element access. HashSet
- implementation of the Set
interface, based on the .hashCode()
function.
该Set
接口表示一组一些对象,无序,没有随机元素访问。HashSet
-Set
接口的实现,基于.hashCode()
功能。
回答by Shiladittya Chakraborty
The HashSet
is an implementation of a Set
.
该HashSet
是的一个实现Set
。
回答by itzmebibin
A Set
represents a generic "set of values". A TreeSe
t is a set where the elements are sorted (and thus ordered), a HashSet
is a set where the elements are not sorted or ordered.
ASet
代表一个通用的“值集”。甲TreeSe
t是一组其中元素进行排序(并因此订购),一个HashSet
是一组,其中的元素是不能的进行排序。
A HashSet
is typically a lot faster than a TreeSet
.
AHashSet
通常比 a 快得多TreeSet
。
Refer this.
参考这个。