当前位置:网站首页>Set接口和Set子实现类
Set接口和Set子实现类
2022-06-23 07:46:00 【Liang FuFu】
1.Set接口
1.Set接口特点:不包含重复元素,元素唯一
无序性
2.Set和List的存取方式一样
2.HashSet集合
1.当调用HashSet的add()方法存入元素时,会先调用hashCode()方法,获取哈希值,根据对象的哈希值计算出一个存储位置,如果位置没有元素,则存入;如果该位置有元素,则调用equals()方法让当前存入的元素和该位置上的元素进行比较.返回false,将元素存入集合,返回true,则将元素舍弃.
自定义类型需要重写hashCode()和equals()方法
3.TreeSet集合
1.底层依赖TreeMap,TreeMap底层数据结构为红黑树结构
2.使用不同的构造方法,会选择不同的排序方式
自然排序:TreeSet(),接口Comparable,重写compareTo方法
比较器排序:TreeSet(Comparator<E> comparator)
接口Comparator,compare方法
3.1 自然排序实现代码
public class Student implements Comparable<Student>{
private String name;
private int age;
//此处有参构造,无参构造,get/set方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写compareTo方法
public int compareTo(Student s){
int num = this.age - s.age ;//由小到大
int num2 = (num==0)?(this.name.compareTo(s.name)):num ;
return num2;
}
}
3.2 比较器排序实现代码
方式一:定义子实现类
public class MyComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
}
TreeSet<Student> ts = new TreeSet<>(new MyComparator()) ;
方式二:匿名内部类
TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getAge() - s1.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
}) ;
方式三:拉姆达表达式
TreeSet<Student> ts = new TreeSet<>((s1,s2) ->{
int num = s2.getAge() - s1.getAge() ;
int num2 = (num==0)?(s1.getName().compareTo(s2.getName())):num ;
return num2;
}
) ;
4.泛型
<?>:任意java类型
<? extends E>:向下限定,E这个类型以及他的子类
<? super E>:向上限定,E这个类型以及他的父类
5.ArrayList集合嵌套
public class Test{
main(){
ArrayList<ArrayList<Student>> bigArray = new ArrayList<>();
ArrayList<Student> array1 = new ArrayList<>() ;
//创建小集合
ArrayList<Student> array1 = new ArrayList<>() ;
ArrayList<Student> array1 = new ArrayList<>() ;
Student s1 = new Student("刘备",35) ;
Student s2 = new Student("曹操",40) ;
array1.add(s1) ;
array1.add(s2) ;
//将小集合添加到大集合中
bigArray.add(array1) ;
//第二个子集合
ArrayList<Student> array2 = new ArrayList<>() ;
//里面添加两个学生数据
Student s3 = new Student("唐僧",50) ;
Student s4 = new Student("孙悟空",42) ;
array2.add(s3) ;
array2.add(s4) ;
//将小集合添加到大集合中
bigArray.add(array2) ;
//第三个子集合
ArrayList<Student> array3 = new ArrayList<>() ;
//里面添加两个学生数据
Student s5 = new Student("宋江",39) ;
Student s6 = new Student("西门庆",28) ;
array3.add(s5) ;
array3.add(s6) ;
//将小集合添加到大集合中
bigArray.add(array3) ;
//方便遍历---->增强for
//当前大集合的数据类型: ArrayList<ArrayList<Student>>
System.out.println("学生的信息如下");
for(ArrayList<Student> myArray:bigArray){
//子集合ArrayList<Student> --->里面的数据类型就是Student
for(Student s :myArray){
System.out.println("\t"+s.getName()+"\t"+s.getAge());
}
}
边栏推荐
猜你喜欢
随机推荐
快速排序 + 冒泡排序 + 插入排序 + 选择排序
QT project error: -1: error: cannot run compiler 'clang++' Output:mingw32-make. exe
js中的同步和异步
Check the file through the port
Playwirght getting started
Fillet the tabbar with the flutter
domain controller
黄蓉真的存在吗?
Kwai 350014
firewalld 配置文件的位置
Create an orderly sequence table and perform the following operations: 1 Insert element x into the table and keep it in order; 2. find the element with the value of X, and delete it if found; 3. outpu
Socket programming (multi process)
google常用语法
2022山东大学软件学院软件项目管理期末考试(回忆版)
1. probability theory - combination analysis
2022 final examination of software project management of School of software, Shandong University (recall version)
Gif verification code analysis
深度学习------不同方法实现vgg16
@Controller和@RestController的区别?
一秒钟查看一次文件,并将文件最后一行内容结果发送至syslog服务器









