当前位置:网站首页>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
边栏推荐
- How many securities companies can a person open an account? Is it safe to open an account
- Modern programming language: rust
- [black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation
- 独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
- golang 猴子吃桃子,求第一天桃子的数量
- Introduction to data warehouse
- Esxi based black Qunhui DSM 7.0.1 installation of VMware Tools
- 怎样能低成本构建一个电商平台
- 单晶炉导电滑环的应用范围和作用是什么
- 信息学奥赛一本通 1359:围成面积
猜你喜欢

Redis configuration and optimization of NoSQL
Latest MySQL advanced SQL statement Encyclopedia

N methods of data De duplication using SQL

Introduction to data warehouse

FB、WhatsApp群发消息在2022年到底有多热门?

The limits of Technology (11): interesting programming

Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!

无人机专用滑环定制要求是什么

What is the e-commerce conversion rate so abstract?

Redis主从复制、哨兵模式、集群的概述与搭建
随机推荐
打新债注册账户安全吗,会有风险吗?
Message Oriented Middleware for girlfriends
What is promise
Taro---day2---编译运行
Matlb| optimal configuration of microgrid in distribution system based on complex network
What is a better and safer app for securities companies to buy stocks
Startup and shutdown of Oracle Database
Is it safe to open a stock account online now? Select a listed securities firm, and the fastest time to open an account is 8 minutes
LATEX 表格内容居左,中,右
.mp4视频测试地址
Overview and deployment of GFS distributed file system
JVM的内存模型简介
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
Code neatness -- format
MySQL 18: execution of write statements
Cloud native O & M article plan
手机股票开户安全吗,买股票在哪开户?
【MySQL】-【函数】
Leetcode 720. The longest word in the dictionary
每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!