目录
一. set集和的特点:
1.set集合是无序的没有下标所以没有修改的方法:
2. 不可重复对象
foreach
迭代器
三、Set去重原理
代码测试
输出结果
四、Set 排序
目录
1.自然排序接口
2.比较器排序接口
一. set集和的特点:
set集合是无序的没有下标所以没有修改的方法:
1. 不可重复对象
输出结果:
二、Set遍历
foreach
输出结果:
迭代器
输出结果 :
三、Set去重原理:
先比较hashCode再比较equals,set集合去重底层是调用hashCode方法以及equals,如果hashCode相等就会调用equals方法,如果不同的话就不会调用
去重是要先经过hashCode的值来筛选,如果两个值都相等的话就会被判定为同一个对象
代码测试:
package com.xiaoye.xy; import java.util.HashSet; public class xy3 { public static void main(String[] args) { HashSet h=new HashSet<>(); h.add(new qc(1,"xg",15)); h.add(new qc(2,"xy",18)); h.add(new qc(3,"xh",16)); h.add(new qc(4,"xz",12)); System.out.println(h.contains(new qc(2,"xy",18))); } } class qc{ private int id; private String name; private int 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "qc [id=" + id + ", name=" + name + ", age=" + age + "]"; } public qc(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public qc() { super(); } @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 + ((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; qc other = (qc) obj; if (age != other.age) return false; 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; } }
输出结果:
四、Set 排序
1.自然排序接口
- this比较元素对象是进行升序
- 元素对象比较this的进行降序
package com.xiaoye.xy; import java.util.HashSet; public class xy3 { public static void main(String[] args) { HashSet h=new HashSet<>(); h.add(new qc(1,"xg",15)); h.add(new qc(2,"xy",18)); h.add(new qc(3,"xh",16)); h.add(new qc(4,"xz",12)); System.out.println(h.contains(new qc(2,"xy",18))); } } class qc{ private int id; private String name; private int 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "qc [id=" + id + ", name=" + name + ", age=" + age + "]"; } public qc(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public qc() { super(); } @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 + ((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; qc other = (qc) obj; if (age != other.age) return false; 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; } }
输出结果:
这样写会报一个Java.lang.comparable的错是因为实现类后面被存放的对象没有实现implements comparable
正确写法:要是现 implements Comparable<Person>
class Person implements Comparable<Person>{
在进行了排序后代码如下:
package com.xiaoye.xy; import java.util.HashSet; import java.util.TreeSet; public class xy4 { public static void main(String[] args) { HashSet h=new HashSet<>(); h.add(new Person(1,13,"xn",12344)); h.add(new Person(2,15,"xb",12533)); h.add(new Person(3,16,"xc",15466)); h.add(new Person(4,18,"xv",16767)); h.add(new Person(5,19,"xr",46573)); //默认排序 for (Object object : h) { System.out.println(object); } TreeSet t=new TreeSet<>(); //对数据进行加工 for (Object object : h) { t.add(object); } System.out.println("----------------------------------------"); for (Object object : t) { System.out.println(object); } } } class Person implements Comparable<Person>{ private int id; private int age; private String name; private int 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 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; Person other = (Person) 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 String toString() { return "Person [id=" + id + ", age=" + age + ", name=" + name + ", money=" + money + "]"; } public Person(int id, int age, String name, int money) { super(); this.id = id; this.age = age; this.name = name; this.money = money; } public Person() { super(); } @Override public int compareTo(Person o) { // TODO Auto-generated method stub return this.id - o.id; } }
输出结果:
2.比较器排序接口代码如下:
- this比较元素对象是进行升序
- 元素对象比较this的进行降序
package com.xiaoye.xy; import java.util.Comparator; import java.util.HashSet; import java.util.TreeSet; public class xy4 { public static void main(String[] args) { HashSet h=new HashSet<>(); h.add(new Person(1,13,"xn",12344)); h.add(new Person(2,15,"xb",12533)); h.add(new Person(3,16,"xc",15466)); h.add(new Person(4,18,"xv",16767)); h.add(new Person(5,19,"xr",46573)); TreeSet t2=new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return o2.getMoney()-o1.getMoney(); } }); for (Object object : h) { t2.add(object); } for (Object object : t2) { System.out.println(object); } System.out.println("----------------------------------------"); TreeSet t3=new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { int xy=o2.getMoney()-o1.getMoney(); if(xy==0) { return o1.getAge()-o2.getAge(); } return xy; } }); for (Object object : h) { t3.add(object); } for (Object object : t3) { System.out.println(object); } } } class Person implements Comparable<Person>{ private int id; private int age; private String name; private int 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 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; Person other = (Person) 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 String toString() { return "Person [id=" + id + ", age=" + age + ", name=" + name + ", money=" + money + "]"; } public Person(int id, int age, String name, int money) { super(); this.id = id; this.age = age; this.name = name; this.money = money; } public Person() { super(); } @Override public int compareTo(Person o) { // TODO Auto-generated method stub return this.id - o.id; }
输出结果:
————————————————