Set接口
父类是Collect接口。不允许重复元素。
HashSet类
实现Set接口,底层由HashMap实现,当使用构造方法创建一个HashSet对象时,底层会使用HashMap的构造方法创建一个对象给HashSet使用。 只使用HashMap集合对象中的键的位置,值的位置都设置为null。代码复用。
集合特点
无序;(只能通过迭代器遍历)
无下标;
键唯一;
允许null元素;
基本使用
构造方法
无参构造:源代码直接使用返回的是一个 HashMap 类型对象
// 构造一个新的空集合; 背景HashMap实例具有默认初始容量(16)和负载因子(0.75)。
HashSet()
// 构造一个新的空集合; 背景HashMap实例具有指定的初始容量和指定的负载因子
HashSet(int initialCapacity, float loadFactor)
方法:此类中没有单个查询的方法 也没有修改的方法,只能遍历得到元素。
add()
作用:向列表中添加元素。
参数:元素Object;
返回值:布尔值
示例:
// 创建HashSet列表
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
System.out.println("reine = " + reine); // reine = true
clear()
作用:清空列表元素。
参数:无;
返回值:无
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 清空列表
hashSet.clear();
contains()
作用:列表内是否包含元素。
参数:Object;
返回值:布尔值
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 列表中包含与否
boolean res = hashSet.contains("Reine");
System.out.println("res = " + res);
isEmpty()
作用:列表是否为空。
参数:无;
返回值:布尔值
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 列表是否为空
boolean empty = hashSet.isEmpty();
System.out.println("empty = " + empty); // false
remove()
作用:删除列表中的元素。
参数:object元素;
返回值:布尔值,成功为true,否则为false
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 列表中删除元素
boolean res = hashSet.remove("Reine");
System.out.println("res = " + res); // res = true
iterator()
作用:获取迭代器对象
参数:无;
返回值:迭代器Iterator对象;
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 遍历集合
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
String item = iterator.next();
System.out.println("item = " + item);
}
size()
作用:获取列表中元素数量
参数:无;
返回值:int类型;
示例:
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 获取列表中数量
int size = hashSet.size();
System.out.println("size = " + size);
遍历集合
方式:通过迭代器方式遍历集合
// 创建HashSet列表 指定键的泛型为String
HashSet<String> hashSet = new HashSet<String>();
// 向列表中添加元素
boolean reine = hashSet.add("Reine");
// 遍历集合
Iterator<String> iterator = hashSet.iterator();
while (iterator.hasNext()){
String item = iterator.next();
System.out.println("item = " + item);
}
方式:通过增强for遍历
// 通过增强for遍历
for (String s : hashSet) {
System.out.println("s = " + s);
}
源码解析
构造函数
// 直接new一个HashMap对象
public HashSet() {
map = new HashMap<>();
}
add方法直接调用put方法
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
LinkedHashSet类
-
LinkedHashSet继承自HashSet类。
-
LinkedHashSet底层实现代码复用的是LinkedHashMap类的代码。
-
HashSet类中的方法都可以使用。
基本使用
构造方法
// 构造一个具有默认初始容量(16)和负载因子(0.75)的新的,空的链接散列集
LinkedHashSet()
// 构造具有指定的初始容量和负载因子的新的,空的链接散列集
LinkedHashSet(int initialCapacity, float loadFactor)
方法
add()
作用:向列表中添加元素。
参数:元素Object;
返回值:布尔值
示例:
// 创建对象
LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
// 添加元素
linkedHashSet.add("Reine");
// 遍历集合
Iterator<String> iterator = linkedHashSet.iterator();
while (iterator.hasNext()){
String item = iterator.next();
System.out.println("item = " + item); // item = Reine
}
...
方法的使用如同LinkedHashMap类中的方法一致。(方法名有所不同,使用的仍是HashSet方法名格式)