标签:唯一性 功能 bsp this asn new span port int
public int hashCode()
public boolean equals(Object obj)
package com.xuweiwei; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class SetDemo { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("hello"); set.add("world"); for(Iterator<String> iterator = set.iterator();iterator.hasNext();){ String str = iterator.next(); System.out.println("str = " + str); } System.out.println("------------------------"); for (String s : set) { System.out.println("s = " + s); } } }
package com.xuweiwei; import java.util.Objects; /** * @author 许威威 * @version 1.0 */ public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.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 Objects.equals(name, student.name) && Objects.equals(age, student.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.HashSet; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class SetDemo2 { public static void main(String[] args) { Set<Student> set = new HashSet<>(); set.add(new Student("张三",20)); set.add(new Student("李四",20)); set.add(new Student("张三",20)); set.add(new Student("李四",30)); set.add(new Student("王五",20)); set.add(new Student("王五",35)); set.add(new Student("王五",25)); for (Student student : set) { System.out.println("student = " + student); } } }
package com.xuweiwei; import java.util.LinkedHashSet; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class LinkedHashSetDemo { public static void main(String[] args) { Set<String> set = new LinkedHashSet<>(); set.add("hello"); set.add("world"); set.add("java"); set.add("hello"); for (String s : set) { System.out.println("s = " + s); } } }
package com.xuweiwei; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Integer> set = new TreeSet<>(); set.add(20); set.add(18); set.add(23); set.add(22); set.add(17); set.add(24); set.add(19); set.add(18); set.add(24); for (Integer i : set) { System.out.println("i = " + i); } } }
package com.xuweiwei; /** * @author 许威威 * @version 1.0 */ public class Student implements Comparable<Student> { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Student o) { int num = this.getAge() - o.getAge(); return num == 0 ? this.getName().compareTo(o.getName()) : num; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<>(); set.add(new Student("张三",20)); set.add(new Student("张三",30)); set.add(new Student("李四",20)); set.add(new Student("王五",90)); set.add(new Student("赵六",7)); set.add(new Student("田七",10)); set.add(new Student("王八",35)); for (Student student : set) { System.out.println("student = " + student); } } }
package com.xuweiwei; /** * @author 许威威 * @version 1.0 */ public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int num = o1.getAge() - o2.getAge(); return num == 0 ? o1.getName().compareTo(o2.getName()) : num; } }); set.add(new Student("张三", 20)); set.add(new Student("张三", 30)); set.add(new Student("李四", 20)); set.add(new Student("王五", 90)); set.add(new Student("赵六", 7)); set.add(new Student("田七", 10)); set.add(new Student("王八", 35)); for (Student student : set) { System.out.println("student = " + student); } } }
标签:唯一性 功能 bsp this asn new span port int
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9350923.html