当前位置:网站首页>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
边栏推荐
- 电商转化率这么抽象,到底是个啥?
- Flutter SliverAppBar全解析,你要的效果都在这了!
- The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
- Every time I started the service of the project, the computer helped me open the browser, revealing the 100 lines of source code!
- 券商买股票用什么app是比较好的,比较安全的
- #795 Div.2 E. Number of Groups set *
- Cloud assisted privacy collection intersection (server assisted psi) protocol introduction: Learning
- Redis configuration and optimization of NoSQL
- 给女朋友看的消息中间件
- 打新债注册账户安全吗,会有风险吗?
猜你喜欢

How to build dual channel memory for Lenovo Savior r720

【说明】Jmeter乱码的解决方法

Esxi based black Qunhui DSM 7.0.1 installation of VMware Tools

Modern programming languages: zig

Meituan dynamic thread pool practice idea has been open source
![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]

Proe/creo product structure design - continuous research

Taro--- day2--- compile and run

Electron window background transparent borderless (can be used to start the page)

Taro---day2---编译运行
随机推荐
From small to large, why do you always frown when talking about learning
Some problems in awk
What is the e-commerce conversion rate so abstract?
[untitled]
Official announcement! Apache Doris graduated from the Apache incubator and officially became the top project of Apache!
Taro---day1---搭建项目
plot_ Model error: pydot and graphviz are not installed
LabVIEW连续采样与有限采样模式
How to build an e-commerce platform at low cost
最新MySQL高级SQL语句大全
PostgreSQL setting auto increment field
什么是过孔式导电滑环?
#796 Div.2 F. Sanae and Giant Robot set *
Electron window background transparent borderless (can be used to start the page)
Alchemy (8): parallel development and release
The contents of the latex table are left, middle and right
plot_model报错:没有安装pydot, graphviz
力扣今日题-522. 最长特殊序列
Huawei partners and Developers Conference 2022 | Kirin software cooperates with Huawei to jointly build the computing industry and create a digital intelligence future
为什么要选择不锈钢旋转接头