当前位置:网站首页>5.Set接口与实现类
5.Set接口与实现类
2022-08-09 09:23:00 【过来我的小熊】
Set接口与实现类
Set子接口
概述
特点:无序,无下标,元素不可重复
方法:全部继承自Collection中的方法
Set接口的使用
package com.jhkj.set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* set方法的使用
*/
public class Demo1 {
public static void main(String[] args) {
// 创建集合
Set<String> set = new HashSet<>();
// 1.添加数据
set.add("华为");
set.add("小米");
set.add("苹果");
System.out.println("元素个数" + set.size());
System.out.println(set.toString());
// 2.删除数据
set.remove("华为");
System.out.println(set.toString());
// 3.遍历数据
System.out.println("--------使用增强for----------");
for (String s : set) {
System.out.println(s);
}
System.out.println("-------使用迭代器------------");
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
// 4.判断
System.out.println(set.contains("华为"));
System.out.println(set.isEmpty());
}
}
Set实现类
注意:使用什么比较就可以重写什么方法
- HashSet:
- 存储结构:哈希表 (数组 + 链表 + 红黑树)
- 存储过程:(重复依据)
- 根据Hashcode计算保存的位置,如果此位置为空,则直接保存,如果不为空则执行第二步
- 再执行equals方法,如果equeals方法为true,则认为是重复,否则,形成链表
- 基于HashCode计算元素存放位置
- 当存入元素的哈希码相同时,会调用equals进行确认,若结果返回true,则拒绝后者存入
- TreeSet:
- Comparable(可比较) Comparator(比较器)
- 存储结构:红黑树
- 要求:
- 元素(类)必须要实现Comparable接口
- 判断 如果compareTo() 方法的返回值为 0 ,则认识是重复元素
- 基于排列顺序实现元素不重复
- 实现了SortedSet接口,对集合元素自动排序
- 元素对象的类型必须实现Comparable接口,指定排序规则
- 通过CompareTo方法确定是否为重复元素
实现比较器
package com.jhkj.set;
import java.util.Comparator;
import java.util.TreeSet;
/**
* Comparator 比较器
* Comparatble 可比较的
* 使用Comparator实现可定制的比较器
*/
public class Demo6 {
public static void main(String[] args) {
// 创建对象
TreeSet<Person> set = new TreeSet<>(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
int n1 = o1.getName().compareTo(o2.getName());
int n2 = o1.getAge() - o2.getAge();
return n1==0?n2:n1;
}
});
Person p1 = new Person("xyz", 11);
Person p2 = new Person("hello", 12);
Person p3 = new Person("zero", 13);
set.add(p1);
set.add(p2);
set.add(p3);
System.out.println("元素个数:" + set.size());
System.out.println(set.toString());
}
}
使用TreeSet集合实现字符串按照长度进行排序
package com.jhkj.set;
import java.util.Comparator;
import java.util.TreeSet;
/**
* TreeSet案例
* 使用TreeSet集合实现字符串按照长度进行排序
*/
public class Demo7 {
public static void main(String[] args) {
TreeSet<String> strings = new TreeSet<>(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;
}
});
strings.add("beijing");
strings.add("pingzi");
strings.add("helloworold");
strings.add("car");
System.out.println("元素个数:" + strings.size());
System.out.println(strings.toString());
}
}
边栏推荐
- 功能自动化测试实施的原则以及方法有哪些?
- 本体开发日记05-努力理解SWRL(下)
- Do you know the principles of test cases and how to write defect reports?
- Amplify Shader Editor手册 Unity ASE(中文版)
- 本体开发日记05-努力理解SWRL(RDF Concrete Syntax)
- 使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(7.4 Annotation Properties-注释属性)
- 这12个GIS软件一个比一个好用
- MySQL Checking and Filling Leaks (5) Unfamiliar Knowledge Points
- 秒拍app分析
- [Environmental Construction] tensorrt
猜你喜欢
随机推荐
MySQL查漏补缺(四)存储过程和游标
Web请求原理
Global 19 Google Satellite Map Free View Download
字符串
“摄像头用不了”+win8.1+DELL+外置摄像头+USB免驱的解决办法
jfinal加载配置文件原理
Anti App so层对抗分析
本体开发日记05-努力理解SWRL(下)
[Pytorch] Install mish_cuda
makefile学习-解决目标文件输出路径问题
A first look at the code to start, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, the first time to run the golang program EP01
Read file by byte and character_load configuration file
What does the test plan include?What is the purpose and meaning?
初窥门径代码起手,Go lang1.18入门精炼教程,由白丁入鸿儒,首次运行golang程序EP01
测试用例的原则、缺陷报告怎么写你都知道吗?
接口开发规范及测试工具的使用
JS报错-Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on...
Do you know the basic process and use case design method of interface testing?
"The camera can't be used" + win8.1 + DELL + external camera + USB drive-free solution
通用的测试用例编写大全(登录测试/web测试等)