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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:42:59  来源:igfitidea点击:

Difference between Set and HashSet in Java

java

提问by user3640657

What is the difference between Setand Hash Setin java? When to use either of these?

java 中的Set和 和有什么区别Hash Set?何时使用其中任何一个?

回答by saroff

Setis 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 Setinterface represents a set of some objects, non-ordered, without random-element access. HashSet- implementation of the Setinterface, based on the .hashCode()function.

Set接口表示一组一些对象,无序,没有随机元素访问。HashSet-Set接口的实现,基于.hashCode()功能。

回答by Shiladittya Chakraborty

The HashSetis an implementation of a Set.

HashSet是的一个实现Set

回答by itzmebibin

A Setrepresents a generic "set of values". A TreeSet is a set where the elements are sorted (and thus ordered), a HashSetis a set where the elements are not sorted or ordered.

ASet代表一个通用的“值集”。甲TreeSet是一组其中元素进行排序(并因此订购),一个HashSet是一组,其中的元素是不能的进行排序。

A HashSetis typically a lot faster than a TreeSet.

AHashSet通常比 a 快得多TreeSet

Refer this.

参考这个