1、Set集合的特点
1.Set集合存储的内容是不可重复的 2.Set集合是无序的
2.、 Set集合的遍历方式
public static void main(String[] args) { Set s = new HashSet(); s.add("a"); s.add("c"); s.add("b"); for (Object o : s) { System.out.println(o); } } • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9
public static void main(String[] args) { Set s = new HashSet(); s.add("a"); s.add("c"); s.add("b"); Iterator it = s.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • 10
3、HashSet哈希表存储、重复元素储存底层探究
首先重写Hashcode,equals方法,调用Hashcode方法对比hash值,再调用equals方法进行对比,如果内容一至,者不添加数据 • 1
实例:
package com.yuan.set; import java.util.HashSet; public class demo1 { public static void main(String[] args) { HashSet set = new HashSet<>(); set.add(new Student(1, "a")); set.add(new Student(2, "b")); set.add(new Student(3, "c")); set.add(new Student(2, "b")); System.out.println(set); } } class Student{ private int id; private String name; public Student() { // TODO Auto-generated constructor stub } public Student(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • 10 • 11 • 12 • 13 • 14 • 15 • 16 • 17 • 18 • 19 • 20 • 21 • 22 • 23 • 24 • 25 • 26 • 27 • 28 • 29 • 30 • 31 • 32 • 33 • 34 • 35 • 36 • 37 • 38 • 39 • 40 • 41 • 42 • 43 • 44 • 45 • 46 • 47 • 48 • 49 • 50 • 51 • 52 • 53 • 54 • 55 • 56 • 57 • 58
4、TreeSet(自然排序、比较急排序)
案例: • 1
package com.yuan.set; import java.util.Comparator; import java.util.HashSet; import java.util.TreeSet; public class demo2 { public static void main(String[] args) { HashSet set = new HashSet<>(); set.add(new Student(1,15, "a",20)); set.add(new Student(2,12, "b",18)); set.add(new Student(3, 11,"c",18)); set.add(new Student(4,11, "b",2222)); // for (Object object : set) { // System.out.println(object); // } //对数据进行加工,按照id升序 TreeSet ts = new TreeSet<>(); for (Object object : set) { ts.add(object); } for (Object object : ts) { System.out.println(object); } System.out.println("--------------------------"); TreeSet tspuls = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub return o2.getMoney()-o1.getMoney(); } }); for (Object object : set) { tspuls.add(object); } //在按照id升序的前提下,按照金钱降序 for (Object object : tspuls) { System.out.println(object); } System.out.println("-------------------"); TreeSet setpuls2 = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { // TODO Auto-generated method stub //在id默认的升序下,进行金钱降序,在金钱相等的情况下,按照年龄升序 int num = o2.getMoney()-o1.getMoney(); if(num==0) { return o1.getAge()-o2.getAge(); } return num; } }); for (Object object : set) { setpuls2.add(object); } //在按照id升序的前提下,按照金钱降序 for (Object object : setpuls2) { System.out.println(object); } } } class Student implements Comparable<Student>{ private int id; private int age; private String name; private int money; public Student() { // TODO Auto-generated constructor stub } public Student(int id, int age, String name, int money) { super(); this.id = id; this.age = age; this.name = name; this.money = money; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "Student [id=" + id + ", age=" + age + ", name=" + name + ", money=" + money + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + id; result = prime * result + money; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (id != other.id) return false; if (money != other.money) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } @Override public int compareTo(Student o) { // TODO Auto-generated method stub return this.id-o.id; } } • 1 • 2 • 3 • 4 • 5 • 6 • 7 • 8 • 9 • 10 • 11 • 12 • 13 • 14 • 15 • 16 • 17 • 18 • 19 • 20 • 21 • 22 • 23 • 24 • 25 • 26 • 27 • 28 • 29 • 30 • 31 • 32 • 33 • 34 • 35 • 36 • 37 • 38 • 39 • 40 • 41 • 42 • 43 • 44 • 45 • 46 • 47 • 48 • 49 • 50 • 51 • 52 • 53 • 54 • 55 • 56 • 57 • 58 • 59 • 60 • 61 • 62 • 63 • 64 • 65 • 66 • 67 • 68 • 69 • 70 • 71 • 72 • 73 • 74 • 75 • 76 • 77 • 78 • 79 • 80 • 81 • 82 • 83 • 84 • 85 • 86 • 87 • 88 • 89 • 90 • 91 • 92 • 93 • 94 • 95 • 96 • 97 • 98 • 99 • 100 • 101 • 102 • 103 • 104 • 105 • 106 • 107 • 108 • 109 • 110 • 111 • 112 • 113 • 114 • 115 • 116 • 117 • 118 • 119 • 120 • 121 • 122 • 123 • 124 • 125 • 126 • 127 • 128 • 129 • 130 • 131 • 132 • 133 • 134 • 135 • 136 • 137 • 138 • 139 • 140 • 141 • 142 • 143 • 144 • 145 • 146 • 147 • 148 • 149