当前位置:网站首页>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
边栏推荐
- 【说明】Jmeter乱码的解决方法
- From small to large, why do you always frown when talking about learning
- Overview and construction of redis master-slave replication, sentinel mode and cluster
- MySQL 18: execution of write statements
- What is a through-hole conductive slip ring?
- Some problems in awk
- How to build an e-commerce platform at low cost
- Is the stock investment exchange group safe? Is it reliable to open an account for free?
- Deepmind | pre training of molecular property prediction through noise removal
- Why are cloud vendors targeting this KPI?
猜你喜欢

Cloud assisted privacy collection intersection (server assisted psi) protocol introduction: Learning

【无标题】

How to add live chat in your Shopify store?

LabVIEW continuous sampling and limited sampling mode

Proe/Creo产品结构设计-钻研不断

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

Meituan dynamic thread pool practice idea has been open source

Squid proxy server (Web cache layer for cache acceleration)

单晶炉导电滑环的应用范围和作用是什么

云厂商为什么都在冲这个KPI?
随机推荐
Is it safe to open an account online now? Novice is just on the road, ask for the answer
Mongodb- install a mongodb database locally on the windows computer
Some habits of making money in foreign lead
#795 Div.2 E. Number of Groups set *
[open source] open source system sorting - Examination Questionnaire, etc
What is the e-commerce conversion rate so abstract?
剑指 Offer 65. 不用加减乘除做加法
Electron window background transparent borderless (can be used to start the page)
Golang monkeys eat peaches and ask for the number of peaches on the first day
哪个证券公司炒股开户是比较好,更安全靠谱的
Leetcode 720. The longest word in the dictionary
大尺寸导电滑环市场应用强度如何
无人机专用滑环定制要求是什么
Official announcement! Apache Doris graduated from the Apache incubator and officially became the top project of Apache!
Is it reliable to invest in exchange traded ETF funds? Is it safe to invest in exchange traded ETF funds
Class文件结构和字节码指令集
Huawei partners and Developers Conference 2022 | Kirin software cooperates with Huawei to jointly build the computing industry and create a digital intelligence future
Why stainless steel swivel
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
How to build dual channel memory for Lenovo Savior r720