集合Set
- 确定性:对任何对象都能判定其是否属于某一个集合;
- 互异性:集合内每个元素都是不相同的,内容互异;
- 无序性:集合内的顺序无关;
Java中的集合接口Set
- HashSet(基于散列函数的集合,无序,不支持同步)
- TreeSet(基于树结构的集合,可排序的,不支持同步)
- LinkedHashSet(基于散列函数和双向链表的集合,可排序的,不支持同步)
HashSet
- 基于HashMap实现的,可以容纳null元素,不支持同步
- Set s=Collections.synchronizedSet(new HashSet(...));
- add 添加一个元素
- clear 清除整个HashSet
- contains 判定是否包含一个元素
- remove 删除一个元素
- size 大小
- retainsAll 计算两个集合交集
import
测试结果
6
7
7
============for循环遍历==============
null
40000
3
20
6
1000
5000000
============测试集合交集==============
交集是 [c]
============测试多种遍历方法速度==============
============迭代器遍历==============
59033300纳秒
============for-each遍历==============
96651700纳秒
- 对集合进行元素重复添加无效
- 遍历的输出顺序与元素添加顺序无关
- 迭代器遍历效率更高
LinkedHashSet
- 继承HashSet,也是基于HashMap实现的,可以容纳null元素
- 不支持同步
- Set s = Collection.synchronizedSet(New LinkedHashSet(...));
- 方法和HashSet基本一致
- add,clear,contains,remove,size
- 通过一个双向链表维护插入顺序
import
测试结果
6
7
7
============for循环遍历==============
null
1000
20
3
40000
5000000
6
============迭代器遍历==============
12353400纳秒
============for-each遍历==============
9365600纳秒
- 遍历结果和插入顺序一致
- for-each遍历更快
TreeSet
- 基于TreeMap实现,不可以容纳null元素,不支持同步
- SortedSet s=Collections.synchronizedSortedSet(new TreeSet(...));//变成同步
- add 添加一个元素
- clear清除整个TreeSet
- contains判断是否包含一个元素
- remove删除一个元素
- size大小
- 根据compareTo方法或指定Comparator排序
import
输出结果:
5
6
6
============for循环遍历==============
3
6
20
1000
40000
5000000
============迭代器遍历==============
8987700纳秒
============for-each遍历==============
6676000纳秒
TreeSet按照所存储对象大小升序输出的
元素重复的判定
- HashSet,LinkedHashSet,TreeSet的元素都只能是对象;
- HashSet和LinkedHashSet判定元素是否重复原则:
- 判定两个元素hashCode返回值是否相同,若不同,返回false;
- 若两者hashCode相同,判定equals方法,若不同,返回false;否则,返回true;
- hashCode和equals方法是所有类都有的,因为Object类有
- TreeSet判定元素重复的原则
- 需要元素继承自Comparable接口
- 比较两个元素的compareTo方法
HashSet和LinkedHashSet例程:
Cat.java
class
Dog.java
class
Tiger.java
public
ObjectHashSetTest
import
输出:
5
6
6
============for循环遍历==============
3
6
20
1000
40000
5000000
============迭代器遍历==============
8987700纳秒
============for-each遍历==============
6676000纳秒
PS C:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemo> cd 'c:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemo'; & 'C:Userszhang.vscodeextensionsvscjava.vscode-java-debug-0.25.1scriptslauncher.bat' 'C:Program FilesJavajdk-12.0.2binjava.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:8842' '-Dfile.encoding=UTF-8' '-cp' 'C:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemobin' 'HashSetJudgeRuleTest'
4
PS C:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemo> cd 'c:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemo'; & 'C:Userszhang.vscodeextensionsvscjava.vscode-java-debug-0.25.1scriptslauncher.bat' 'C:Program FilesJavajdk-12.0.2binjava.exe' '-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:8863' '-Dfile.encoding=UTF-8' '-cp' 'C:UserszhangDocumentsVS CodeJavaJavaCoreTechniqueSetDemobin' 'ObjectHashSetTest'
==========Cat HashSet ==============
6
========================
6
==========Dog HashSet ==============
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog equals()~~~~~~~~~~~
5
========================
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog hashCode()~~~~~~~~~~~
Dog equals()~~~~~~~~~~~
5
==========Tiger HashSet ==============
6
========================
6
这三个方法三位一体:
- equals()是相同的
- hashCode()是相同的
- toString()也应该是相同的
TreeSet例程
import
结果
==========Tiger TreeSet ==============
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
Tiger compareTo()~~~~~~~~~~~
5