集合操作工具类Colletions

 

 

 

 

 

 1 import java.util.ArrayList;
 2 import java.util.Collections;
 3 import java.util.List;
 4 
 5 public class ColletionsDemo1 {
 6     public static void main(String[] args) {
 7 
 8         // 1. 批量加数据
 9         List<String> names = new ArrayList<>();
10         //names.add("wl");
11         //names.add("phx");
12         Collections.addAll(names, "wl", "phx", "xw", "xp");
13         System.out.println(names);
14 
15         // 2. 打乱List集合顺序
16         Collections.shuffle(names);
17         System.out.println(names);
18 
19         // 3. 将集合中元素按照默认规则排序(只能排值特性的元素)
20         List<Integer> list = new ArrayList<>();
21         Collections.addAll(list, 33, 21 ,5, 1, 4, 6, 22, 2);
22         System.out.println(list);
23         Collections.sort(list);
24         System.out.println(list);
25     }
26 }

 

posted @ 2022-07-29 15:52  小王同学学编程  阅读(20)  评论(0编辑  收藏  举报
levels of contents