当前位置:网站首页>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
边栏推荐
- Ten MySQL locks, one article will give you full analysis
- 电商转化率这么抽象,到底是个啥?
- Code neatness -- function
- Why stainless steel swivel
- DeepMind | 通过去噪来进行分子性质预测的预训练
- 快速掌握grep命令及正则表达式
- #796 Div.2 F. Sanae and Giant Robot set *
- 联想拯救者R720如何组建双通道内存
- 1696D. Permutation Graph 思维
- Is it safe to open a new bond registration account? Is there any risk?
猜你喜欢

Leetcode 720. The longest word in the dictionary

SPuG - lightweight automatic operation and maintenance platform

云厂商为什么都在冲这个KPI?

Proe/creo product structure design - continuous research

Class文件结构和字节码指令集

Logging log usage

每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!

What are cookies and the security risks of v-htm

Modern programming language: rust

Proe/Creo产品结构设计-钻研不断
随机推荐
Message Oriented Middleware for girlfriends
Internship: business process introduction
What is the e-commerce conversion rate so abstract?
Taro--- day2--- compile and run
How many securities companies can a person open an account? Is it safe to open an account
The limits of Technology (11): interesting programming
信息学奥赛一本通 1359:围成面积
哪个证券炒股开户佣金是最便宜,最安全的
Electron window background transparent borderless (can be used to start the page)
SQL Server 2016 detailed installation tutorial (with registration code and resources)
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
每次启动项目的服务,电脑竟然乖乖的帮我打开了浏览器,100行源码揭秘!
Which securities speculation account opening commission is the cheapest and safest
股票投资交流群安全吗?入群免费开户靠谱嘛?
Code neatness -- function
On charsequence
Acwing第 57 场周赛【未完结】
#796 Div.2 C. Manipulating History 思维
IIC communication protocol for single chip microcomputer
如何在您的Shopify商店中添加实时聊天功能?