Set系列集合特点
哈希值
HashSet集合的底层原理
HashSet集合去重复
代码
代码一:整体了解一下Set系列集合的特点
package com.itheima.day20_Collection_set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
//目标:整体了解一下Set系列集合的特点。
public class SetTest1 {
public static void main(String[] args) {
// 1、创建一个Set集合的对象
//Set<Integer> set = new HashSet<>();// 创建了一个HashSet的集合对象。一行经典代码 HashSet: 无序 不重复 无索引
//Set<Integer> set = new LinkedHashSet<>();//有序 不重复 无索引
Set<Integer> set = new TreeSet<>();//可排序(升序) 不重复 无索引
set.add(666);
set.add(666);
set.add(666);
set.add(888);
set.add(888);
set.add(888);
set.add(777);
set.add(777);
System.out.println(set);
}
}
代码二:了解一下哈希值。Java中的所有对象,都可以调用0bejct类提供的hashcode方法,返回该对象自己的哈希值
Student类(学生类)
package com.itheima.day20_Collection_set;
import java.util.Objects;
public class Student {
private String name;
private int age;
private double height;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
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;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
SetTest2 类(主程序)
package com.itheima.day20_Collection_set;
/*目标:了解一下哈希值。Java中的所有对象,都可以调用0bejct类提供的hashcode方法,返回该对象自己的哈希值
public int hashCode():返回对象的哈希值。
同一个对象多次调用hashcode()方法返回的哈希值是相同的。
不同的对象,它们的哈希值一般不相同,但也有可能会相同(哈希碰撞)。*/
public class SetTest2 {
public static void main(String[] args) {
Student s1 = new Student("飞鸟马时",17,165);
Student s2 = new Student("枣伊吕波",17,155);
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
String str1 = new String("abc");
String str2 = new String("acD");
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
}
}
代码三:自定义的类型的对象,比如两个内容一样的学生对象,如何让HashSet集合能够去重复
Student类(学生类)
package com.itheima.day20_Collection_set;
import java.util.Objects;
public class Student {
private String name;
private int age;
private double height;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
//只要两个对象内容一样就返回true
@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 && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
// 姓名 年龄 身高计算哈希值的
return Objects.hash(name, age, height);
}
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
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;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
SetTest3 (主程序)
package com.itheima.day20_Collection_set;
import java.util.HashSet;
import java.util.Set;
//目标:自定义的类型的对象,比如两个内容一样的学生对象,如何让HashSet集合能够去重复!
public class SetTest3 {
public static void main(String[] args) {
Student s1 = new Student("飞鸟马时",17,165);
Student s2 = new Student("枣伊吕波",17,155);
Student s3 = new Student("枣伊吕波",17,155);
Set<Student> students = new HashSet<>();
students.add(s1);
students.add(s2);
students.add(s3);
System.out.println(students.toString());
}
}