当前位置:网站首页>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());
}
}
边栏推荐
猜你喜欢

PHP serialization and deserialization CTF

Imperva- method of finding regular match timeout

深度学习------不同方法实现vgg16

PHP 文件包含 -ctf

船长阿布的灵魂拷问

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

How to start Jupiter notebook in CONDA virtual environment
![Acwing game 56 [End]](/img/f6/cd650331c819a27f17c9ce6cd0c569.png)
Acwing game 56 [End]

聊聊服务治理中的路由设计

Apache Solr 任意文件读取复现
随机推荐
Display proportion of sail soft accumulation diagram
Gif verification code analysis
Rotary table visual screening machine and its image recognition system
MySQL系统表介绍
Socket programming -- select model
The sandbox has reached a cooperation with football player to bring popular football cartoons and animation into the metauniverse
ArcMap批量删除距离较近的点
flutter 制作TabBar的圆角
Sequence table Curriculum
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
抓包发现tcp会话中老是出现重复的ack和大量的tcp重传——SACK(Selective Acknowledgment, 选择性确认)技术
力扣(LeetCode)173. 二叉搜索树迭代器(2022.06.22)
Socket socket programming
Imperva- method of finding regular match timeout
QT irregular shape antialiasing
openvino系列 19. OpenVINO 与 PaddleOCR 实现视频实时OCR处理
Match 56 de la semaine d'acwing [terminé]
Configuration asmx not accessible
[try to hack] IP address
MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2