From 5a4df5e6f3fb2814ba2f646f2ca6b21390e7b000 Mon Sep 17 00:00:00 2001 From: seahi Date: Sun, 1 Dec 2024 22:36:03 +0800 Subject: [PATCH] =?UTF-8?q?HashSet=E5=92=8CTreeSet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chapter6/Example08.java | 73 +++++++++++++++++++++++++++++++++++++ src/chapter6/Example12.java | 21 +++++++++++ src/chapter6/Example14.java | 4 ++ src/chapter6/Example15.java | 4 ++ src/chapter6/Example19.java | 4 ++ src/chapter6/Example21.java | 4 ++ src/chapter6/Example22.java | 4 ++ src/chapter6/Example23.java | 4 ++ src/chapter6/Example24.java | 4 ++ src/chapter6/Example25.java | 4 ++ 10 files changed, 126 insertions(+) create mode 100644 src/chapter6/Example08.java create mode 100644 src/chapter6/Example12.java create mode 100644 src/chapter6/Example14.java create mode 100644 src/chapter6/Example15.java create mode 100644 src/chapter6/Example19.java create mode 100644 src/chapter6/Example21.java create mode 100644 src/chapter6/Example22.java create mode 100644 src/chapter6/Example23.java create mode 100644 src/chapter6/Example24.java create mode 100644 src/chapter6/Example25.java diff --git a/src/chapter6/Example08.java b/src/chapter6/Example08.java new file mode 100644 index 0000000..719fb47 --- /dev/null +++ b/src/chapter6/Example08.java @@ -0,0 +1,73 @@ +package chapter6; + +import java.util.HashSet; +import java.util.Iterator; + +class Student implements Comparable { + 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:表示"我"和"你"一样大 + } + } +} + +public class Example08 { + public static void main(String[] args) { + HashSet 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(s3); + students.add(s4); + students.add(s1); + students.add(s2); + + Iterator iterator = students.iterator(); + while (iterator.hasNext()) { + Student student = iterator.next(); + System.out.println(student); + } + + } +} diff --git a/src/chapter6/Example12.java b/src/chapter6/Example12.java new file mode 100644 index 0000000..7f2fd8b --- /dev/null +++ b/src/chapter6/Example12.java @@ -0,0 +1,21 @@ +package chapter6; + +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 students = new TreeSet<>(); + students.add(s1); + students.add(s2); + students.add(s4); + + System.out.println(students); + } +} diff --git a/src/chapter6/Example14.java b/src/chapter6/Example14.java new file mode 100644 index 0000000..6885386 --- /dev/null +++ b/src/chapter6/Example14.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example14 { +} diff --git a/src/chapter6/Example15.java b/src/chapter6/Example15.java new file mode 100644 index 0000000..68c18fd --- /dev/null +++ b/src/chapter6/Example15.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example15 { +} diff --git a/src/chapter6/Example19.java b/src/chapter6/Example19.java new file mode 100644 index 0000000..34bb4c6 --- /dev/null +++ b/src/chapter6/Example19.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example19 { +} diff --git a/src/chapter6/Example21.java b/src/chapter6/Example21.java new file mode 100644 index 0000000..dafc24b --- /dev/null +++ b/src/chapter6/Example21.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example21 { +} diff --git a/src/chapter6/Example22.java b/src/chapter6/Example22.java new file mode 100644 index 0000000..e47a284 --- /dev/null +++ b/src/chapter6/Example22.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example22 { +} diff --git a/src/chapter6/Example23.java b/src/chapter6/Example23.java new file mode 100644 index 0000000..3ab2320 --- /dev/null +++ b/src/chapter6/Example23.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example23 { +} diff --git a/src/chapter6/Example24.java b/src/chapter6/Example24.java new file mode 100644 index 0000000..b9ef937 --- /dev/null +++ b/src/chapter6/Example24.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example24 { +} diff --git a/src/chapter6/Example25.java b/src/chapter6/Example25.java new file mode 100644 index 0000000..93a6035 --- /dev/null +++ b/src/chapter6/Example25.java @@ -0,0 +1,4 @@ +package chapter6; + +public class Example25 { +}