6.2 HashSet和TreeSet

系列 - JAVA集合

本实验手册主要内容是HashSet、LinkedHashSet和TreeSet

java

import java.util.HashSet;  

public class Example08 {  
    public static void main(String[] args) {  
        HashSet<String> names = new HashSet<>();  
        
        names.add("张三");  
        names.add("李四");  
        names.add("王五");  
        names.add("李四");  
  
        System.out.println(names);  
    }  
}

java

import java.util.HashSet;  
  
class Student {  
    String id;  
    String name;  
  
    public Student(String id, String name) {  
        this.id = id;  
        this.name = name;  
    }  
  
    public String toString() {  
        return id + ":" + name;  
    }   
}  
  
public class Example08 {  
    public static void main(String[] args) {  
        HashSet<Student> students = new HashSet<>();  
        Student s1 = new Student("1", "张三");  
        Student s2 = new Student("2", "李四");  
        Student s3 = new Student("2", "李四");  
        Student s4 = new Student("3", "王五"); 
  
        students.add(s1);  
        students.add(s2);  
        students.add(s3);  
        students.add(s4); 
  
        System.out.println(students);  
    }  
}

修改后:

java

import java.util.HashSet;  
  
class Student {  
    String id;  
    String name;  
  
    public Student(String id, String name) {  
        this.id = id;  
        this.name = name;  
    }  
  
    public String toString() {  
        return id + ":" + name;  
    }  
  
    public int hashCode() {  
        return id.hashCode();  
    }  

	// 判断自己和obj是不是相等
    public boolean equals(Object obj) {  
	    // 如果obj就是自己
        if (this == obj) {  
            return true;  
        }  
		// 如果 obj 不是 Student 类型
        if (!(obj instanceof Student)) {  
            return false;  
        }  
		// 把 obj 转换成 Student(强制类型转换)
        Student other = (Student) obj;  
        // 我们认为学号相等就是同一个学生
        return this.id.equals(other.id);  
    }  
}  
  
// 其他地方不变

运行结果:

把刚才代码中的 HashSet 改成 LinkedHashSet

java

import java.util.TreeSet;

public class Example11 {
   public static void main(String[] args) {
       TreeSet ts = new TreeSet();
       ts.add(3);
       ts.add(1);
       ts.add(1);
       ts.add(2);
       ts.add(3);
       System.out.println(ts);
   }
}

运行结果:

java

import java.util.TreeSet;  
  
public class Example12 {  
    public static void main(String[] args) {  
        Student s1 = new Student("1", "张三");  
        s1.setOrder(4);  
        Student s2 = new Student("2", "李四");  
        s2.setOrder(3);  
        Student s4 = new Student("3", "王五");  
        s4.setOrder(1);  
  
        TreeSet<Student> students = new TreeSet<>();  
        students.add(s1);  
        students.add(s2);  
        students.add(s4);  
  
        System.out.println(students);  
    }  
}

java

class Student implements Comparable<Student> {  
    String id;  
    String name;  
    int height;  // 身高
  
    public Student(String id, String name) {  
        this.id = id;  
        this.name = name;  
    }  
      
    public void setHeight(int height) {  
        this.height = height;  
    }  
  
    public String toString() {  
        return id + ":" + name;  
    }  
  
    public int hashCode() {  
        return id.hashCode();  
    }  
  
    public boolean equals(Object obj) {  
        if (this == obj) {  
            return true;  
        }  
  
        if (!(obj instanceof Student)) {  
            return false;  
        }  
  
        Student other = (Student) obj;  
        return this.id.equals(other.id);  
    }  
  
    public int compareTo(Student other) {  
        if (this.height > other.height) {  
            return 1; // 返回正数:表示"我"比"你"大  
        } else if (this.height < other.height) {  
            return -1;  // 返回负数:表示"我"比"你"小  
        } else {  
            return 0; // 返回0:表示"我"和"你"一样大  
        }  
    }  
}

java

import java.util.TreeSet;  
  
public class Example12 {  
    public static void main(String[] args) {  
        Student s1 = new Student("1", "张三");  
        s1.setHeight(175);  
        Student s2 = new Student("2", "李四");  
        s2.setHeight(170);  
        Student s4 = new Student("3", "王五");  
        s4.setHeight(180);  
  
        TreeSet<Student> students = new TreeSet<>();  
        students.add(s1);  
        students.add(s2);  
        students.add(s4);  
  
        System.out.println(students);  
    }  
}

运行结果:

相关内容