当前位置:网站首页>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
边栏推荐
- Cloud native O & M article plan
- [description] solution to JMeter garbled code
- FB、WhatsApp群发消息在2022年到底有多热门?
- Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
- What is the e-commerce conversion rate so abstract?
- 网页鼠标点击特效案例收集(直播间红心同理)
- How to add live chat in your Shopify store?
- How about the market application strength of large-size conductive slip rings
- Alchemy (8): parallel development and release
- PostgreSQL设置自增字段
猜你喜欢

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

独立站卖家都在用的五大电子邮件营销技巧,你知道吗?

为什么要选择不锈钢旋转接头

Web3 技术初体验以及相关学习资料

Leetcode 720. The longest word in the dictionary

去哪儿网(Qunar) DevOps 实践分享

. Mp4 video test address

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

LabVIEW continuous sampling and limited sampling mode

Message Oriented Middleware for girlfriends
随机推荐
golang 猴子吃桃子,求第一天桃子的数量
Installation and use of Zotero document management tool
Set集合用法
哪个证券炒股开户佣金是最便宜,最安全的
股票投资交流群安全吗?入群免费开户靠谱嘛?
What are cookies and the security risks of v-htm
#796 Div.2 C. Manipulating History 思维
同花顺上能炒股开户吗?安全吗?
Taro--- day2--- compile and run
想开户买股票,在网上办理股票开户安全吗?
1696D. Permutation Graph 思维
795 div.2 D. Max GEQ sum monotone stack
MySQL 18: execution of write statements
免费、好用、强大的开源笔记软件综合评测
【MySQL】-【函数】
大尺寸导电滑环市场应用强度如何
Ten MySQL locks, one article will give you full analysis
同花顺股票开户是会有什么风险吗?同花顺开户安全吗
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
Ceiling scheme 1