当前位置:网站首页>Set collection usage
Set collection usage
2022-06-28 01:23:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
nature
The bottom layer adopts hash table algorithm , Disorder cannot be repeated
Array weight removal
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));
}Gather to get rid of the heavy
List<String> list = new ArrayList<>();
list.add("aa");
list.add("aa");
list.add("bb");
list.add("cc");
list.add("cc");
System.out.println(" Before de duplication list:"+list);
Set<String> set2= new HashSet<>();
set2.addAll(list);
System.out.println("set2:"+set2);
list.clear();
list.addAll(set2);
System.out.println(" After weight removal list:"+list);TreeSet Automatic sorting
Entity classes must implement Comparable Interface and override CompareTo Method , Write collation ( Ascending 、 Descending )
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();
}
}Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/133014.html Link to the original text :https://javaforall.cn
边栏推荐
- IIC communication protocol for single chip microcomputer
- Message Oriented Middleware for girlfriends
- 【说明】Jmeter乱码的解决方法
- The contents of the latex table are left, middle and right
- 现在网上开股票账户安全吗?选择上市券商,最快8分钟开户成功
- Deep parsing of kubernetes controller runtime
- Zhang Fan: the attribution of flying pig after advertising based on causal inference technology
- Which securities speculation account opening commission is the cheapest and safest
- #796 Div.2 C. Manipulating History 思维
- Taro--- day1--- construction project
猜你喜欢

The flutter slivereappbar is fully parsed. All the effects you want are here!

electron窗口背景透明无边框(可用于启动页面)

Deep parsing of kubernetes controller runtime

Solon 1.8.3 发布,云原生微服务开发框架

大尺寸导电滑环市场应用强度如何

什么是过孔式导电滑环?
![The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]](/img/56/768f8be9f70bf751f176e40cbb1df2.png)
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]

What are cookies and the security risks of v-htm

Electron window background transparent borderless (can be used to start the page)
![[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science
随机推荐
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
The contents of the latex table are left, middle and right
[description] solution to JMeter garbled code
Is it safe to open an account for mobile stocks? Where can I open an account for buying stocks?
【DNS 解析】将Name.com的域名接入DNSPod解析
手机股票开户安全吗,买股票在哪开户?
#796 Div.2 F. Sanae and Giant Robot set *
What is a through-hole conductive slip ring?
Redis主从复制、哨兵模式、集群的概述与搭建
Why stainless steel swivel
MySQL - function
给女朋友看的消息中间件
股票投资交流群安全吗?入群免费开户靠谱嘛?
电商转化率这么抽象,到底是个啥?
MySQL十种锁,一篇文章带你全解析
券商买股票用什么app是比较好的,比较安全的
联想拯救者R720如何组建双通道内存
Some problems in awk
Class文件结构和字节码指令集
同花顺上能炒股开户吗?安全吗?