- 1、Set 接口和常用方法
- 1.1、Set 接口基本介绍
- 1.2、Set 接口的常用方法
- 1.3、Set 接口的遍历方式
- 2、Set 接口实现类-HashSet
- 2.1、HashSet 的全面说明
- 2.2、HashSet 的 小练习
- 2.3、HashSet 底层机制说明
- 2.4、HashSet 练习题
和 List 接口一样,Set 接口也是 Collection 的子接口,因此,常用方法和 Collection 接口一样
package set_;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
@SuppressWarnings({"all"})
public class SetMethod {
public static void main(String[] args) {
// 1. 以 Set 接口的实现类 HashSet 来讲解 Set 接口的方法
// 2. set 接口的实现类的对象(Set 接口对象), 不能存放重复的元素, 可以添加一个 null
// 3. set 接口对象存放数据是无序(即添加的顺序和取出的顺序不一致)
// 4. 注意: 取出的顺序的顺序虽然不是添加的顺序, 但是他是固定的
Set set = new HashSet();
set.add("john");
set.add("lucy");
set.add("john"); // 重复
set.add("jack");
set.add("tom");
set.add("mary");
set.add(null);
set.add(null); // 再次添加 null
for (int i = 0; i < 10; i++) {
System.out.println("set=" + set);
}
// 遍历
// 方式 1: 使用迭代器
System.out.println("===使用迭代器===");
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj=" + obj);
}
set.remove(null);
// 方式 2: 增强 for
System.out.println("===增强 for===");
for (Object o : set) {
System.out.println("o=" + o);
}
// set 接口对象, 不能通过索引来获取
}
}
1.3、Set 接口的遍历方式
2、Set 接口实现类-HashSet
2.1、HashSet 的全面说明
1. 构造器走的源码
public HashSet() {
map = new HashMap<>();
}
2. HashSet 可以存放 null, 但是只能有一个 null, 即元素不能重复
Set hashSet = new HashSet();
hashSet.add(null);
hashSet.add(null);
System.out.println("hashSet=" + hashSet); // hashSet=[null]
2.2、HashSet 的 小练习
package set_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class HashSet01 {
public static void main(String[] args) {
HashSet set = new HashSet();
// 说明
// 1. 在执行 add 方法后, 会返回一个 boolean 值
// 2. 如果添加成功, 返回 true, 否则返回 false
// 3. 可以通过 remove 指定删除哪个对象
System.out.println(set.add("john")); // true
System.out.println(set.add("lucy")); // true
System.out.println(set.add("john")); // false
System.out.println(set.add("jack")); // true
System.out.println(set.add("Rose")); // true
set.remove("john");
System.out.println("set=" + set); // set=[Rose, lucy, jack]
set = new HashSet();
System.out.println("set=" + set); // set=[]
// 4. Hashset 不能添加相同的元素/数据
set.add("lucy"); // 添加成功
set.add("lucy"); // 加入不了
set.add(new Dog("tom")); // 添加成功
set.add(new Dog("tom")); // 添加成功
System.out.println("set=" + set); // set=[Dog{name='tom'}, Dog{name='tom'}, lucy]
// 非常经典的面试题
// 看源码, 即 add 到底发生了什么 => 底层机制
set.add(new String("allen")); // 添加成功
set.add(new String("allen")); // 加入不了
System.out.println("set=" + set); // set=[allen, Dog{name='tom'}, Dog{name='tom'}, lucy]
}
}
class Dog { // 定义了 Dog 类
private String name;
public Dog(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + ''' +
'}';
}
}
2.3、HashSet 底层机制说明
package set_;
import java.util.HashMap;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class HashSetSource {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add("java"); // 到此位置, 第 1 次 add 分析完毕
hashSet.add("php"); // 到此位置, 第 2 次 add 分析完毕
hashSet.add("java");
System.out.println("set=" + hashSet); // set=[java, php]
// 对 HashSet 的源码解读
}
}
package set_;
import java.util.HashSet;
@SuppressWarnings({"all"})
public class HashSetIncrement {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
for (int i = 0; i < 100; i++) {
hashSet.add(i); // 1, 2, 3, ... 100
}
for (int i = 0; i < 12; i++) {
hashSet.add(new A(i));
}
for (int i = 1; i <= 7; i++) { // 在 table 的某一条链表上添加了 7 个 A 对象
hashSet.add(new A(i));
}
for (int i = 0; i < 7; i++) { // 在 table 的另外一条链表上添加了 7 个 B 对象
hashSet.add(new B(i));
}
}
}
class A {
private int n;
public A(int n) {
this.n = n;
}
@Override
public int hashCode() {
return 100;
}
}
class B {
private int n;
public B(int n) {
this.n = n;
}
@Override
public int hashCode() {
return 200;
}
}
2.4、HashSet 练习题
package set_;
import java.util.HashSet;
import java.util.Objects;
@SuppressWarnings({"all"})
public class HashSetExercise {
public static void main(String[] args) {
HashSet hashSet = new HashSet();
hashSet.add(new Employee("milan", 18));
hashSet.add(new Employee("smith", 28));
hashSet.add(new Employee("milan", 18));
System.out.println("hashSet=" + hashSet); // hashSet=[Employee{name='smith', age=28}, Employee{name='milan', age=18}]
}
}
// 创建 Employee
class Employee {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
// 如果 name 和 age 值相同, 则返回相同的 hash 值
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return age == employee.age && name.equals(employee.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}



