- HashSet(重点):
- 基于HashCode计算元素存放的位置
- 当存入元素的哈希码相同时,会调用equals进行确认,如果结果为true,则拒绝后者存入
- 存储结构:哈希表(数组+链表+红黑树)
- 存储过程(重复的依据):
- 根据hashcode计算保存的位置,如果此位置为空,则直接保存,否则执行第二步
- 再执行equals方法,如果equals方法为true,则认为是重复,否则形成链表
- HashSet存储数据实际上用的是HashMap的key来存储的
//创建集合
HashSet<String> hashSet = new HashSet<String>();
//添加元素
hashSet.add("小米");
hashSet.add("苹果");
hashSet.add("华为");
hashSet.add("华为"); //不可重复
System.out.println("数据个数:" + hashSet.size()); //数据个数:3 不重复
System.out.println(hashSet.toString()); //[苹果,华为,小米] 无序
//遍历
//增强for
for(String s : hashSet){
System.out.println(s);
}
//迭代器
Iterator<String> it = hashSet.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//判断
System.out.println(hashSet.contains("OPPO")); //false
System.out.println(hashSet.isEmpty()); //false
//删除数据
hashSet.remove("苹果");
System.out.println(hashSet.toString()); //[华为,小米]
//学生类
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
}
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 "Student [name=" + name + ", age=" + age + "]";
}
//自动生成的重写方法
@Override
public int hashCode() {
//31是一个质数,减少散列冲突
//31提高执行效率 31*i=(i<<5)-i
final int prime = 31;
int result = 1;
result = prime * result + age;
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;
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;
}
//自己写的重写方法
/*
@Override
public int hashCode(){
int n1 = this.name.hashCode();
int n2 = this.age;
return n1+n2;
}
@Override
public boolean equals(Object obj){
//判断是不是同一个对象
if(this == obj){
return true;
}
//判断是否是空
if(obj == null){
return false;
}
//判断是否是Student类型
if(obj instanceof Student){
Student s = (Student)obj;
//比较属性
if(this.name.equals(s.getName()) && this.age == s.getAge()){
return true;
}
}
//不满足条件返回false
return false;
}
*/
}
//HashSet使用:保存学生信息
public class Test{
public static void main(String[] args){
//创建集合
HashSet<Student> hashSet = new HashSet<Student>();
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",21);
Student s3 = new Student("王五",22);
//添加数据
hashSet.add(s1);
hashSet.add(s2);
hashSet.add(s3);
hashSet.add(new Student("王五",22)); //重写hashCode、equals方法后无法添加进去,重复
System.out.println("元素个数:" + hashSet.size()); //元素个数:3
//遍历
//1.增强for循环
for(Student s : hashSet){
System.out.println(s.toString());
}
//2.迭代器
Iterator<Student> it = hashSet.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//判断
System.out.println(hashSet.contains(s1)); //true
System.out.println(hashSet.contains(new Student("李四",21))); //重写hashCode、equals方法后返回true
System.out.println(hashSet.isEmpty()); //false
//删除数据
hashSet.remove(s1);
System.out.println("删除之后:" + hashSet.size()); //删除之后:2
hashSet.remove(new Student("李四",21)); //重写hashCode、equals方法才能实现删除
System.out.println("删除之后:" + hashSet.size()); //删除之后:1
}
}
- TreeSet
- 基于排列顺序实现元素不重复
- 实现了SortedSet接口,对集合元素自动排序
- 元素对象的类型必须实现Comparable接口,指定排序规则
- 存储结构:红黑树
- TreeSet实际上是用的TreeMap的key来保存的值
//创建集合
TreeSet<String> treeSet = new TreeSet<String>();
//添加元素
treeSet.add("xyz");
treeSet.add("abc");
treeSet.add("hello");
treeSet.add("abc"); //重复不添加
System.out.println("元素个数:" + treeSet.size()); //元素个数:3
System.out.println(treeSet.toString()); //[abc,hello,xyz]
//遍历
//增强for
for(String s : treeSet){
System.out.println(s);
}
//迭代器
Iterator<String> it = treeSet.iterator();
while(it.hasNext()){
System.out.println(it,.next());
}
//判断
System.out.println(treeSet.contains("xyz")); //true
System.out.println(treeSet.isEmpty()); //false
//删除
treeSet.remove("xyz");
System.out.println("删除之后:" + treeSet.size()); //删除之后:2
//学生类
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
}
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 "Student [name=" + name + ", age=" + age + "]";
}
//先按姓名比,再按年龄比
@Override
public int compareTo(Student o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.age - o.getAge();
return n1==0?n2:n1;
}
}
//TreeSet使用:保存学生信息
//元素必须要实现Comparable接口,compareTo()返回值为0即重复
public class Test{
public static void main(String[] args){
//创建集合
TreeSet<Student> treeSet = new TreeSet<Student>();
Student s1 = new Student("张三",20);
Student s2 = new Student("李四",21);
Student s3 = new Student("王五",22);
//添加数据
treeSet.add(s1);
treeSet.add(s2);
treeSet.add(s3);
treeSet.add(new Student("王五",22)); //实现Comparable接口后无法添加进去,重复
System.out.println("元素个数:" + treeSet.size()); //元素个数:3
//遍历
//1.增强for循环
for(Student s : treeSet){
System.out.println(s.toString());
}
//2.迭代器
Iterator<Student> it = treeSet.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
//判断
System.out.println(treeSet.contains(s1)); //true
System.out.println(treeSet.contains(new Student("李四",21))); //实现Comparable接口后返回true
System.out.println(treeSet.isEmpty()); //false
//删除数据
treeSet.remove(s1);
System.out.println("删除之后:" + treeSet.size()); //删除之后:2
treeSet.remove(new Student("李四",21)); //实现Comparable接口才能实现删除
System.out.println("删除之后:" + treeSet.size()); //删除之后:1
}
}
-
Comparator接口
- 实现定制比较(比较器)
//创建集合并指定比较规则,不用使用元素的比较规则了 TreeSet<Student> treeSet = new TreeSet<Student>(new Comparator<Student>(){ //先比年龄后比姓名 @Override public int compare(Student o1,Student o2){ int n1 = o1.getAge() - o2.getAge(); int n2 = o1.getName() - o2.getAge(); return n1==0?n2:n1; } }); Student s1 = new Student("张三",20); Student s2 = new Student("李四",21); Student s3 = new Student("王五",22); //添加数据 treeSet.add(s1); treeSet.add(s2); treeSet.add(s3); treeSet.add(new Student("王五",22)); //实现Comparator接口的compare方法后无法添加进去,重复 System.out.println("元素个数:" + treeSet.size()); //元素个数:3
TreeSet<String> treeSet = new TreeSet<String>(new Comparator<String>(){ //按照字符串长度进行比较 @Override public int compare(String o1,String o2){ int n1 = o1.length() - o2.length(); int n2 = o1.compareTo(o2); return n1==0?n2:n1; } });