Set集合
参考视频:13.23 Set集合概述哔哩哔哩bilibili
Set实现类
HashSet的使用
-
存储结构:哈希表(数组+链表+红黑树)
-
存储过程:
1.根据hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空,执行第二步;
2.再执行equals方法,如果equals方法为true,则认为是重复,否则,形成链表;
代码如下:
import java.util.HashSet;
import java.util.Iterator;
public class Demo1 {
public static void main(String[] args) {
HashSet<String> hash = new HashSet<>();
hash.add("苹果");
hash.add("香蕉");
hash.add("鸭梨");
hash.add("香蕉");
System.out.println(hash.size());
System.out.println(hash.toString());//输出[苹果, 香蕉, 鸭梨],重复元素不输出
//遍历
Iterator<String> it = hash.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
//删除与判断
hash.remove("香蕉");
System.out.println(hash.size());//输出2
System.out.println(hash.contains("香蕉"));//输出false
System.out.println(hash.isEmpty());//输出false
}
}
TreeSet:
一、使用comparable接口
//创建实体类StudentTree.java
package com.mokuiran.set;
public class StudentTree implements Comparable<StudentTree>{
private String name;
private int age;
public StudentTree() {
}
public StudentTree(String name, int age) {
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 "StudentTree{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(StudentTree o) {
int n1 = this.getName().compareTo(o.getName());
int n2 = this.age-o.getAge();
return n1==0?n2:n1;//返回值为0,则认为是重复元素
}
}
//测试
package com.mokuiran.set;
import java.util.TreeSet;
public class TreeSetDemo1 {
public static void main(String[] args) {
//创建集合
TreeSet<StudentTree> tr = new TreeSet<>();
//1.添加元素
StudentTree s1 = new StudentTree("xiaoming", 11);
StudentTree s2 = new StudentTree("xiaohong", 12);
StudentTree s3 = new StudentTree("xiaoli", 13);
tr.add(s1);
tr.add(s2);
tr.add(s3);
//若要输出,则需要在Student类中实现Comparable方法
System.out.println(tr.size());
System.out.println(tr.toString());//输出[StudentTree{name='xiaohong', age=12}, StudentTree{name='xiaoli', age=13}, StudentTree{name='xiaoming', age=11}]
//删除
tr.remove(new StudentTree("xiaoli",13));//由于有comparable,因此可以删除
System.out.println(tr.size());//输出2
System.out.println(tr.toString());//输出[StudentTree{name='xiaohong', age=12}, StudentTree{name='xiaoming', age=11}]
}
}
二、使用匿名内部类Comparator
//实体类标签:Set,name,int,age,tr,-----,集合,public,StudentTree From: https://www.cnblogs.com/mokuiran/p/16594269.html
package com.mokuiran.set;
public class StudentTree{
private String name;
private int age;
public StudentTree() {
}
public StudentTree(String name, int age) {
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;
}
}
//测试
package com.mokuiran.set;
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo2 {
public static void main(String[] args) {
TreeSet<StudentTree2> tr = new TreeSet<>(new Comparator<StudentTree2>() {
@Override
public int compare(StudentTree2 o1, StudentTree2 o2) {
int n1 = o1.getAge()-o2.getAge();
int n2 = o1.getName().compareTo(o2.getName());
return n1==0?n2:n1;
}
});
StudentTree2 s1 = new StudentTree2("xiaoming", 11);
StudentTree2 s2 = new StudentTree2("xiaohong", 12);
StudentTree2 s3 = new StudentTree2("xiaoli", 13);
tr.add(s1);
tr.add(s2);
tr.add(s3);
System.out.println(tr.toString());/*输出[StudentTree{name='xiaoming', age=11},
StudentTree{name='xiaohong', age=12}, StudentTree{name='xiaoli', age=13}],由于年龄都不一样,所以
用年龄做了排序*/
}
}