Set
package com.text.entity;
import java.util.*;
import java.util.function.Consumer;
public class A01_SetDemo1 {
public static void main(String[] args) {
//1. 创建一个Set集合的对象
Set<String> s = new HashSet<>();
// 2. 添加元素
// 如果当前元素是第一次添加,那么可以添加成功,返回true
// 如果当前元素是第二次添加,那么添加失败,返回false
s.add("张三");
s.add("张三");
s.add("李四");
s.add("王五");
// 打印集合
// 无序
//System.out.println(s); // [李四, 张三, 王五]
//迭代器遍历
/*Iterator<String> it = s.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}*/
// 增强for
/*for (String str : s) {
System.out.println(str);
}*/
//lambad 表达式遍历
s.forEach(str -> System.out.println(str));
}
}
Student 类
package com.text.entity;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Student{name = " + name + ", age = " + age + "}";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
HashSet
Demo1
package com.text.entity;
public class A02_ShashSetDemo1 {
public static void main(String[] args) {
//1. 创建对象
Student s1 = new Student("zhangsan", 23);
Student s2 = new Student("zhangsan", 21);
// 如果没有重写hashCode方法,不同对象计算出的哈希值是不同的
System.out.println(s1.hashCode()); // -1461067292
System.out.println(s2.hashCode()); // -1461067292
// 哈希碰撞
System.out.println("abc".hashCode()); // 96354
System.out.println("acD".hashCode()); // 96354
}
}
Demo2
package com.text.entity;
import java.util.HashMap;
import java.util.HashSet;
public class A03_HashSetDemo2 {
public static void main(String[] args) {
// 1. 创建三个学生对象
Student s1 = new Student("zhangsan", 23);
Student s2 = new Student("lisi", 24);
Student s3 = new Student("wangwu", 25);
Student s4 = new Student("zhangsan", 23);
// 2. 创建集合用来添加学生
HashSet<Student> hs = new HashSet<>();
// 3. 添加元素
System.out.println(hs.add(s1));
System.out.println(hs.add(s2));
System.out.println(hs.add(s3));
System.out.println(hs.add(s4));
// 4. 打印集合
System.out.println(hs);
}
}
LinkedHashSet
package com.text.entity;
import sun.awt.image.ImageWatched;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
public class A04_LinkedHashSetDemo {
public static void main(String[] args) {
// 1. 创建4个学生对象
Student s1 = new Student("zhangsan", 23);
Student s2 = new Student("lisi", 24);
Student s3 = new Student("wangwu", 25);
Student s4 = new Student("zhangsan", 23);
// 2. 创建集合的对象
LinkedHashSet<Student> lhs = new LinkedHashSet<>();
// 3.添加元素
System.out.println(lhs.add(s1));
System.out.println(lhs.add(s2));
System.out.println(lhs.add(s3));
System.out.println(lhs.add(s4));
// 4. 打印集合(按照添加的顺序)
System.out.println(lhs);
}
}
对于HashSet和LinkedHashSet的选择问题:
1. 默认使用HashSet
2. 如果要求去重且有序,才使用LinkedHashSet