java 集合(Set接口)
Set接口:无序集合,不允许有重复值,允许有null值
存入与取出的顺序有可能不一致
HashSet:具有set集合的基本特性,不允许重复值,允许null值
底层实现是哈希表结构
初始容量为16
保存自定义对象时,保证数据的唯一性,主要由哈希表的结构决定
先判断hashCode()是否相同,若相同,再判断equals()是否相同
哈希表结构:哈希表中存放的都是对象的哈希码值,当想要给哈希表中存放对象时,需要先进行对象的哈希码值比较,若表中已经存在了相同的哈希码,则继续比较equals方法,比较对象的值是否相同,若返回false,则存入哈希表中,相当于两个对象共享同一块内存地址。若表中无相同的哈希码值,则直接存入。
package com.itycl.array; /* * 用HashSet去除重复值 重写toString() hashCode() equals() */ import java.util.HashSet; import java.util.Iterator;
public class HashSetDemo { public static void main(String[] args) {
HashSet<Integer> set=new HashSet<>(); set.add(1); set.add(2); set.add(3); set.add(3); set.add(4); set.add(4); Iterator it=set.iterator(); while(it.hasNext()) { Integer value = (Integer) it.next(); System.out.println(value); } System.out.println(set); } }
|
HashSet存放自定义对象:
package SetDemo1;
import java.util.HashSet; import java.util.Iterator; /*课堂练习: * * * * System.out.println():默认添加: 内容.toString * 输出该对象的字符串表示 * * * */
public class HashSetDemo2 { public static void main(String[] args) { HashSet<Person> ps=new HashSet<>(); ps.add(new Person("小花","18")); ps.add(new Person("小绿","19")); ps.add(new Person("小红","19")); ps.add(new Person("小粉","18"));
ps.add(new Person("小草","19")); ps.add(new Person("小草","19")); Iterator<Person> it=ps.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } |
Person类:
package collection;
public class Person implements Comparable<Person> { private String name; private int 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; } public Person(String name, int age) { super(); this.name = name; this.age = age; } @Override public int compareTo(Person o) { if(this.age>o.age) { return 1; }else if(this.age<o.age) { return -1; }else { return this.getName().compareTo(o.getName()); }
} @Override public String toString() {
return "Person[name="+name+","+"age="+age+"]"+"\t"; } @Override public int hashCode() { return this.name.hashCode()+this.age*7; } @Override public boolean equals(Object obj) {
if(obj==null) { throw new RuntimeException("传入的对象不能为空!"); }
if(!(obj instanceof Person)) { throw new RuntimeException("传入的对象不是Person的实例!"); }
Person p = (Person) obj; return p.getName().equals(this.name) && p.getAge()==(this.age);
} }
|
TreeSet:具有set集合的基本特性,有序集合,不允许重复值,允许null。
底层实现是树的数据结构。
两种排序方式:
a)实现自comparable接口:自然顺序,实现该接口类自身就具备了一种可比较性
*重写compareTo()方法
b)自定义一个比较器comparator:可以作为参数传给set集合,让集合具有明确的比较
1.实现自comparable接口:
package SetDemo1; import java.util.Iterator; import java.util.TreeSet; /*使用TreeSet集合保存自定义对象 * 并且,认为年龄和姓名相同对象为同一个学生对象 * */ public class TreeSetDemo1 { public static void main(String[] args) { TreeSet<Student> ts=new TreeSet<>(); ts.add(new Student("java01",20)); ts.add(new Student("java02",10)); ts.add(new Student("java03",20)); ts.add(new Student("java04",30)); ts.add(new Student("java05",20)); ts.add(new Student("java05",20));//不允许重复值 ts.add(null); Iterator<Student> it=ts.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }
|
Person类:
package com.itycl.array; //实现comparable<T>接口 public class Person implements Comparable<Person> { private String name; private int 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; } public Person(String name, int age) { super(); this.name = name; this.age = age; } //重写compareTo() @Override public int compareTo(Person o) { if(this.age>o.age) { return 1; }else if(this.age<o.age) { return -1; }else { return this.getName().compareTo(o.getName()); }
} //重写toString() @Override public String toString() {
return "Person[name="+name+","+"age="+age+"]"+"\t"; } } |
2.自定义一个比较器:comparator,让集合具有明确的比较
自定义比较器:实现implements Comparator接口
package com.itycl.array; import java.util.Comparator; public class MyComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { System.out.println(o1+":"+o2); if(o1==null || o2==null) { throw new RuntimeException("比较的参数不能为空"); } if(o1.getAge()>o2.getAge()) { return 1; }else if(o1.getAge()<o2.getAge()) { return -1; }else { return o1.getName().compareTo(o2.getName()); }
} } |
Student类:
package com.itycl.array; public class Student { private String name; private int age; 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; } @Override public String toString() { return "Person[name="+name+","+"age="+age+"]"+"\t"; } } |
TreeSetDemo1:
package com.itycl.array; import java.util.Iterator; import java.util.TreeSet; public class TreeSetDemo1 { public static void main(String[] args) { MyComparator my=new MyComparator();//创建比较器对象,并且传到set集合中,使set集合本身具有明确的比较 TreeSet<Student> set=new TreeSet<>(my); set.add(new Student("张倩",12)); set.add(new Student("李雷",10)); set.add(new Student("兰兰",6)); set.add(new Student("老刘",26)); set.add(new Student("王乐",21)); Iterator it=set.iterator(); while(it.hasNext()) { Student s = (Student) it.next(); System.out.println(s.toString()); } } } |