当前位置:网站首页>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
边栏推荐
- Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
- Summary of wuenda's machine learning course (11)_ Support vector machine
- Deep parsing of kubernetes controller runtime
- Quickly master grep commands and regular expressions
- MATLB|基于复杂网络的配电系统微电网优化配置
- Logging log usage
- Esxi based black Qunhui DSM 7.0.1 installation of VMware Tools
- Summary of attack methods of attack team
- Class文件结构和字节码指令集
- Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
猜你喜欢
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
How about the market application strength of large-size conductive slip rings
完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
【无标题】
MySQL 18: execution of write statements
Introduction to memory model of JVM
Squid代理服务器(缓存加速之Web缓存层)
Understand the basic structure of wechat applet project
深入解析kubernetes controller-runtime
Matlb| optimal configuration of microgrid in distribution system based on complex network
随机推荐
Redis主从复制、哨兵模式、集群的概述与搭建
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
Is it safe to open a new bond registration account? Is there any risk?
Software engineering job design (1): [personal project] implements a log view page
Why stainless steel swivel
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
What is a better and safer app for securities companies to buy stocks
electron窗口背景透明无边框(可用于启动页面)
The Internet industry has derived new technologies, new models and new types of industries
Matlb| optimal configuration of microgrid in distribution system based on complex network
MySQL 18: execution of write statements
SQL Server 2016 detailed installation tutorial (with registration code and resources)
Overview and construction of redis master-slave replication, sentinel mode and cluster
Leetcode 720. The longest word in the dictionary
What is the application scope and function of the conductive slip ring of single crystal furnace
【开源】开源系统整理-考试问卷等
Alchemy (6): iteratable models and use cases
哪个证券炒股开户佣金是最便宜,最安全的
Download, configuration and installation of MySQL
#796 Div.2 C. Manipulating History 思维