2019独角兽企业重金招聘Python工程师标准>>>
Set集合
HashSet
哈希表保证元素的唯一性依赖于两个方法一个是hashCode方法一个是equals方法
如果两个对象的hashCode值相同,并且调用该对象的equals方法返回的是true的时候,那么就说明两个对象是相同的
结论:
HashSet集合存储元素,保证元素的唯一性,需要让这个元素重写hashCode和equals方法
遍历hash表
(1)Student.java
public class Student {private String name;private int age;public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, int age) {super();this.name = name;this.age = age;}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;}
//重写hashCode方法@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + age;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}
//重写equals方法@Overridepublic 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 (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}
(2)hashSetTest.java
import java.util.HashSet;public class hashSetTest {public static void main(String[] args) {Student s1 = new Student("卫杰",21);Student s2 = new Student("宋玉",22);Student s3 = new Student("黄英",18);Student s4 = new Student("卫杰",21);HashSet<Student> hs = new HashSet<Student>();hs.add(s1);hs.add(s2);hs.add(s3);hs.add(s4);for(Student s:hs){System.out.println(s.getName()+"---"+s.getAge());}}
}
TreeSet
TreeSet: 可以对元素进行排序 , 而排序分为两种方式一种自然排序 ,一种比较器排序
那么我们到底使用的自然排序还是比较器排序主要取决于构造方法
public TreeSet() 使用的是自然排序
public TreeSet(Comparator comparator) 使用比较器排序
一.使用自然排序
(1)Person.java
public class Person implements Comparable<Person> {private String name;private int age;public Person() {super();// TODO Auto-generated constructor stub}public Person(String name, int age) {super();this.name = name;this.age = age;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}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;}//采用自然排序方式进行排序,实现compareTo方法@Overridepublic int compareTo(Person o) {// TODO Auto-generated method stub//比较年龄int num = this.age -o.age;int num2 = (num == 0) ? this.name.compareTo(o.name) : num;return num2;}}
(2)TreeSetDemo1.java
* 根据年龄对Person类的对象进行排序*/
import java.util.TreeSet;public class TreeSetDemo1 {public static void main(String[] args) {//创建person对象Person p1 = new Person("昭明",22);Person p2 = new Person("清风",16);Person p3 = new Person("兰亭",23);Person p4 = new Person("清风",24);Person p5 = new Person("兰亭集",23);//创建TreeSet对象TreeSet<Person> tr = new TreeSet<Person>(); tr.add(p5);tr.add(p4);tr.add(p3);tr.add(p2);tr.add(p1);for(Person t : tr){System.out.println(t.getName()+"-------"+t.getAge());}}
}/*清风-------16
昭明-------22
兰亭-------23
兰亭集-------23
清风-------24*/
二.使用比较器进行排序
(1)Person.java
public class Person {private String name;private int age;public Person() {super();// TODO Auto-generated constructor stub}public Person(String name, int age) {super();this.name = name;this.age = age;}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;}}
(2)MyComparater.java
//使用比较器进行比较
import java.util.Comparator;public class MyComparater implements Comparator<Person> {@Overridepublic int compare(Person arg0, Person arg1) {// TODO Auto-generated method stub//按照名字长度进行比较int num = arg0.getName().length()-arg1.getName().length();//判断名字是否相同int num2 =(num==0)?arg0.getName().compareTo(arg1.getName()):num;//比较年龄大小int num3 = (num2==0)?arg0.getAge()-arg1.getAge():num2;return num3;}}
(3)TreeSetDemo2.java
*/
import java.util.TreeSet;
import java.util.Comparator;public class TreeSetDemo2 {public static void main(String[] args) {//自定义Person类的对象Person p1 = new Person("Bob",23);Person p2 = new Person("Wiki",22);Person p3 = new Person("Wiki",21);Person p4 = new Person("go",19);Person p5 = new Person("Link",22);//创建TreeSet集合对象TreeSet<Person> tr = new TreeSet<Person>(new MyComparater());//使用内部类实现比较器排序
// TreeSet<Person> tr = new TreeSet<Person>(new Comparator<Person>(){
// public int compare(Person arg0, Person arg1) {
// // TODO Auto-generated method stub
// //按照名字长度进行比较
// int num = arg0.getName().length()-arg1.getName().length();
// //判断名字是否相同
// int num2 =(num==0)?arg0.getName().compareTo(arg1.getName()):num;
// //比较年龄大小
// int num3 = (num2==0)?arg0.getAge()-arg1.getAge():num2;
// return num3;
// }
// });//使用内部类比较器进行比较//将Person类的对象加入TreeSet集合tr.add(p5);tr.add(p4);tr.add(p3);tr.add(p2);tr.add(p1);for(Person t:tr){System.out.println(t.getName()+"---"+t.getAge());}}
}
/** go---19 Bob---23 Link---22 Wiki---21 Wiki---22*/