三.set集合的去重
1.set集合去重原理
先调用hashcode方法进行比较哈希值,哈希值不相同则判断完毕,哈希值相同则再进行调用equals方法判断比较内容并返回结果
package com.YU; import java.util.HashSet; import java.util.Iterator; /** * set集合的去重原理 * 1.先调用hashcode方法 * 2.再调用equals方法 * @author 21879 * */ public class Demo3 { public static void main(String[] args) { //定义set集合 HashSet hs = new HashSet<>(); //添加元素 hs.add(new Person(1, "死崽", 8000)); hs.add(new Person(2, "小刘", 10000)); hs.add(new Person(3, "大鹏", 8000)); System.out.println(hs.contains(new Person(3, "大鹏", 8000))); for (Object object : hs) { System.out.println(object); } } } class Person{ private int id; private String name; private float money; public Person() { // TODO Auto-generated constructor stub } public Person(int id, String name, float money) { super(); this.id = id; this.name = name; this.money = money; } 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 float getMoney() { return money; } public void setMoney(float money) { this.money = money; } @Override public int hashCode() { System.out.println("hashcode被调用"); final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + Float.floatToIntBits(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; Person other = (Person) obj; if (id != other.id) return false; if (Float.floatToIntBits(money) != Float.floatToIntBits(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 String toString() { return "Person [id=" + id + ", name=" + name + ", money=" + money + "]"; } }
运行结果:
hashcode被调用 hashcode被调用 hashcode被调用 hashcode被调用 equals被调用 true Person [id=1, name=死崽, money=8000.0] Person [id=3, name=大鹏, money=8000.0] Person [id=2, name=小刘, money=10000.0]
此段为前面的代码,图中第八行重写了hashcode方法和equals方法,并将名为hs的set集合与对象Person(3, "大鹏", 8000)进行判断,根据运行结果来看前三次都调用了hashcode判断哈希值,第三个哈希值相同,就调用了equals方法,equals方法判断内容与set集合中的内容有重复,返回true,
注意:两个对象内容相同并不代表哈希值相同,每个对象都有一个独有的哈希值,所以最后才需要用到equals方法进行比较
四.set集合的排序
1.通过将Treeset进行排序
package com.YU; import java.util.HashSet; import java.util.Iterator; import java.util.TreeSet; /** * set集合的排序 * @author 21879 * */ public class Demo4 { public static void main(String[] args) { //定义set集合 HashSet hs = new HashSet<>(); //添加元素 hs.add(new Persons(1, "死崽", 8000)); hs.add(new Persons(2, "小刘", 10000)); hs.add(new Persons(3, "大鹏", 8000)); //定义treeset TreeSet ts = new TreeSet<>(); System.out.println("Hashset集合正常排序=============="); for (Object object : hs) { //set集合正常排序 System.out.println(object); } for (Object object : hs) { //将Hashset集合中的数据放到treeset集合中进行加工 ts.add(object); } System.out.println("Treeset加工后集合排序=============="); for (Object object : ts) { System.out.println(object); } } } class Persons implements Comparable<Persons>{ private int id; private String name; private float money; public Persons() { // TODO Auto-generated constructor stub } public Persons(int id, String name, float money) { super(); this.id = id; this.name = name; this.money = money; } 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 float getMoney() { return money; } public void setMoney(float money) { this.money = money; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", money=" + money + "]"; } @Override public int compareTo(Persons o) { return this.id - o.id; } }
运行结果:
Hashset集合正常排序============== Person [id=1, name=死崽, money=8000.0] Person [id=3, name=大鹏, money=8000.0] Person [id=2, name=小刘, money=10000.0] Treeset加工后集合排序============== Person [id=1, name=死崽, money=8000.0] Person [id=2, name=小刘, money=10000.0] Person [id=3, name=大鹏, money=8000.0]
解释:通过对象实现Comparable接口,重写compareTo方法根据id进行排序
2.根据多重条件进行判断
package com.YU; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.TreeSet; /** * set集合的排序 * * @author 21879 * */ public class Demo4 { public static void main(String[] args) { // 定义set集合 HashSet hs = new HashSet<>(); // 添加元素 hs.add(new Persons(1, 18, "小刘", 10000)); hs.add(new Persons(3, 19, "死崽", 8000)); hs.add(new Persons(2, 20, "大鹏", 8000)); // 定义treeset TreeSet ts = new TreeSet<>(); System.out.println("Hashset集合正常排序=============="); for (Object object : hs) { // set集合正常排序 System.out.println(object); } for (Object object : hs) { // 将Hashset集合中的数据放到treeset集合中进行加工 ts.add(object); } TreeSet tspro = new TreeSet<>(new Comparator<Persons>() { @Override public int compare(Persons o1, Persons o2) { int money = (int) (o1.getMoney() - o2.getMoney()); if (money == 0) { return (int) (o1.getAge() - o2.getAge()); } return money; } }); for (Object object : hs) { // 将Hashset集合中的数据放到treeset集合中进行加工 tspro.add(object); } System.out.println("Treeset集合多重条件排序=============="); for (Object object : tspro) { System.out.println(object); } } } class Persons implements Comparable<Persons> { private int id; private int age; private String name; private float money; public Persons() { // TODO Auto-generated constructor stub } public Persons(int id, int age, String name, float money) { super(); this.id = id; this.age = age; this.name = name; this.money = money; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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 float getMoney() { return money; } public void setMoney(float money) { this.money = money; } @Override public String toString() { return "Persons [id=" + id + ", age=" + age + ", name=" + name + ", money=" + money + "]"; } @Override public int compareTo(Persons o) { return 0; } }
运行结果:
Hashset集合正常排序============== Persons [id=1, age=18, name=小刘, money=10000.0] Persons [id=2, age=20, name=大鹏, money=8000.0] Persons [id=3, age=19, name=死崽, money=8000.0] Treeset集合多重条件排序============== Persons [id=3, age=19, name=死崽, money=8000.0] Persons [id=2, age=20, name=大鹏, money=8000.0] Persons [id=1, age=18, name=小刘, money=10000.0]
多重比较排序先对money进行一个升序,若money相同时,则进行年龄的升序排序
以上就是所有内容了,感谢观看