当前位置:网站首页>Set集合用法
Set集合用法
2022-06-27 23:01:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
性质
底层采用哈希表算法,无序不可重复
数组去重
public static String[] removeRepeat(String[] array){
Set<String> set = new HashSet<>();
for(int i = 0; i < array.length; i++){
set.add(array[i]);
}
String[] arr = set.toArray(new String[set.size()]);
return arr;
}
public static void main(String[] args) {
String[] arr = {"java","java","C++","python"};
String[] arr2 = removeRepeat(arr);
System.out.println(Arrays.toString(arr2));
}
集合去重
List<String> list = new ArrayList<>();
list.add("aa");
list.add("aa");
list.add("bb");
list.add("cc");
list.add("cc");
System.out.println("去重前list:"+list);
Set<String> set2= new HashSet<>();
set2.addAll(list);
System.out.println("set2:"+set2);
list.clear();
list.addAll(set2);
System.out.println("去重后list:"+list);
TreeSet自动排序
实体类必须实现Comparable接口并重写CompareTo方法,编写排序规则(升序、降序)
public class SetTest {
public static void main(String[] args) {
Set<Person> set3 = new TreeSet<>();
set3.add(new Person("Daniel",22));
set3.add(new Person("Eddie",21));
set3.add(new Person("Jesska",20));
System.out.println(set3);
}
}
class Person implements Comparable<Person>{
String name;
int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Person arg0) {
return this.age - arg0.getAge();
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133014.html原文链接:https://javaforall.cn
边栏推荐
- 完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
- Introduction to memory model of JVM
- Function and usage of malloc function in C language
- Alchemy (7): how to solve problems? Only reconstruction
- IIC communication protocol for single chip microcomputer
- Distortion model of SDF learning
- Introduction to data warehouse
- #796 Div.2 C. Manipulating History 思维
- How about the market application strength of large-size conductive slip rings
- Proe/Creo产品结构设计-钻研不断
猜你喜欢
Redis主从复制、哨兵模式、集群的概述与搭建
What are the requirements for customizing the slip ring for UAV
Technical debt wall: a way to make technical debt visible and negotiable
电商转化率这么抽象,到底是个啥?
Leetcode 720. The longest word in the dictionary
Overview and deployment of GFS distributed file system
为什么要选择不锈钢旋转接头
What is a through-hole conductive slip ring?
单晶炉导电滑环的应用范围和作用是什么
MATLB|基于复杂网络的配电系统微电网优化配置
随机推荐
剑指 Offer 65. 不用加减乘除做加法
【开源】开源系统整理-考试问卷等
#796 Div.2 C. Manipulating History 思维
AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
golang 猴子吃桃子,求第一天桃子的数量
795 div.2 D. Max GEQ sum monotone stack
#795 Div.2 E. Number of Groups set *
. Mp4 video test address
Redis主从复制、哨兵模式、集群的概述与搭建
电商转化率这么抽象,到底是个啥?
electron窗口背景透明无边框(可用于启动页面)
Is it reliable to invest in exchange traded ETF funds? Is it safe to invest in exchange traded ETF funds
什么是过孔式导电滑环?
Code neatness -- function
Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
1696D. Permutation Graph 思维
Acwing第 57 场周赛【未完结】
从小到大为何一谈学习就愁眉苦脸
How to add live chat in your Shopify store?
美团动态线程池实践思路已开源