【Set集合】
java.util.Set 接口 extends Collection接口
Set接口的特点:
- 不允许存储重复的元素
- 没有索引,没有带索引的方法,也不能使用普遍的for循环遍历
【HashSet】
HashSet特点:
- 不允许存储重复的元素
- 没有索引,没有带索引的方法,也不能使用普遍的for循环遍历
- 是一个无序的集合,存储元素和取出元素的顺序有可能不一致
- 底层是一个哈希表结构(查询的速度非常的快)
public static void main(String[] args) {
Set<Integer> set = new HashSet<>();
// 使用add方法往集合中添加元素
set.add(1);
set.add(3);
set.add(2);
set.add(1);
// 使用迭代器遍历set集合
Iterator<Integer> it = set.iterator();
while(it.hashNext()) {
Integer n = it.next();
System.out.prontln(n);//1,2,3
}
// 使用增强for遍历set集合
for(Ingeter i : set) {
System.out.println(i);
}
}
哈希值:是一个十进制的整数,由系统随机给出(就是对象的地址值,是一个逻辑地址,是模拟出来得到地址,不是数据实际存储的物理地址)在Object类有一个方法,可以获取对象的哈希值
//int hashCode() 返回该对象的哈希码值。
hashCode方法的源码:
public native int hashCode();
//native:代表该方法调用的是本地操作系统的方法


【LinkedHashSet】
java.util.LinkedHashSet集合 extends HashSet集合
LinkedHashSet集合特点:
底层是一个哈希表(数组+链表/红黑树)+链表,多了一条链表(记录元素的存储顺序),保证元素有序
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("www");
set.add("abc");
set.add("abc");
set.add("zwh");
System.out.println(linked);//[abc,www,zwh]
}
LinkedHashSet<String> linked = new LinkedHashSet<>();
linked.add("www");
linked.add("abc");
linked.add("abc");
linked.add("zwh");
System.out.println(linked);//[www,abc,zwh]有序,不允许重复
697

被折叠的 条评论
为什么被折叠?



