一.Set集合的特点
集合都具备的特点就是自带有增删改查,但是set集合没有修改,因为其是无序的!
其特点为:
- 无序 没有下标 因此没有修改
- 不可添加重复项
二.Set集合的遍历方式
因为其没有下标所有就没有fori的遍历方式
遍历方式
- foreach
- iterator
三.set集合去重
底层就是调的实体类重写的hashCode方法和equals方法
首先是字符串形式
对象形式需要调HashCode和equals方法将其重写
class Yinzi{ private int id; private String name; public Yinzi() { // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Yinzi(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Yinzi [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { System.out.println("调用了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) { System.out.println("equals方法被调用了"); if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Yinzi other = (Yinzi) 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; }
注意:先调用hashCode方法进行对比,如果最后返回的结果相同,则再调用equals方法
四.排序
- 自然排序
- 比较器排序
4.1自然排序
实体类实现Comparable 重写compareTo方法
class YZ implements Comparable<YZ>{ private int id; private String name; private int age; private int money; public YZ() { // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 int getMoney() { return money; } public void setMoney(int money) { this.money = money; } public YZ(int id, String name, int age, int money) { super(); this.id = id; this.name = name; this.age = age; this.money = money; } @Override public String toString() { return "YZ [id=" + id + ", name=" + name + ", age=" + age + ", money=" + money + "]"; } @Override public int hashCode() { System.out.println("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) { System.out.println("equals被调用了"); if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; YZ other = (YZ) 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(YZ o) { //根据money降序排序 //return o.money-this.money ; //根据money排序且再按照年龄排序 return o.money-this.money ; }
4.2比较器排序
TreeSet构造器使用实现java.util.Comparator的匿名内部类
//比较器排序 TreeSet ts2=new TreeSet<>(new Comparator<YZ>() { //比较 // @Override // public int compare(YZ o1, YZ o2) { // return o2.getId()-o1.getId(); // } //双重比较 @Override public int compare(YZ o1, YZ o2) { //判断金钱 int num=o2.getMoney()-o1.getMoney(); if(num==0) { //再还要根据年龄排序 return o1.getAge()-o2.getAge(); } return num; } }); //添加 for (Object object : set) { ts2.add(object); } //打印 for (Object object : ts2) { System.out.println("后"+object); } }