目录
- 1、判断列表是否为空或者为NULL
- 2、列表包含
- 3、列表排序
- 4、列表截取
- 5、列表合并
- 6、列表求极值
- 7、列表转字符串
- 8、列表去重的四种方式
- 9、列表转数组
1、判断列表是否为空或者为NULL
Optional.ofNullable(list).orElse(Collections.emptyList()).isEmpty() // true=为空或NULL false 不为空或NULL
2、列表包含
List<String> list = Arrays.asList("apple", "banana", "cherry");
boolean containsApple = list.contains("apple"); // 返回 true
boolean containsMango = list.contains("mango"); // 返回 false
3、列表排序
对象
List<User> users = new ArrayList<>();
users.add(new User(1L, "张三", 18, 1));
users.add(new User(2L, "李四", 52, 1));
users.add(new User(3L, "王五", 36, 1));
users.add(new User(4L, "赵六", 36, 1));
// users.sort(Comparator.comparing(User::getAge)); // 正序
users.sort(Comparator.comparing(User::getId).reversed()); // 倒叙
// users.sort(Comparator.comparing(User::getAge).thenComparing(User::getId)); // 多条件排序
System.out.println(users);
List<Map<>>
List<Map<String, Object>> resultlist = new ArrayList<>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("id", "1");
map1.put("name", "张三");
map1.put("Score", 86.5);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("id", "2");
map2.put("name", "李四");
map2.put("Score", 90.0);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("id", "3");
map3.put("name", "王五");
map3.put("Score", 70.5);
resultlist.add(map1);
resultlist.add(map2);
resultlist.add(map3);
resultlist.sort((o1, o2) -> {
Double one = Double.valueOf(o1.get("Score").toString());
Double two = Double.valueOf(o2.get("Score").toString());
return one.compareTo(two); //one.compareTo(two)为升序,two.compareTo(one)为降序
});
List String
List<String> headList = new ArrayList();
Collections.sort(headList);
4、列表截取
列表截取 — 左闭右开
List<User> users = new ArrayList<>();
users.add(new User(1L, "张三", 18, 1));
users.add(new User(2L, "李四", 52, 1));
users.add(new User(3L, "王五", 36, 1));
users.add(new User(4L, "赵六", 36, 1));
List<User> new_users = users.subList(0, 3);
for (User newUser : new_users) {
System.out.println(newUser);
}
5、列表合并
List<Integer> list1 = Arrays.asList(1, 2, 3);
List<Integer> list2 = Arrays.asList(4, 5, 6);
List<Integer> list3 = Arrays.asList(7, 8, 9);
List<Integer> mergedList = Stream.of(list1, list2, list3).flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(mergedList);// [1, 2, 3, 4, 5, 6, 7, 8, 9]
6、列表求极值
注意:Collections是java.util包下的
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer max = Collections.max(list);
Integer min = Collections.min(list);
System.out.println("最大值:" + max + "最小值:" + min);
7、列表转字符串
// 创建一个字符串列表
List<String> list = Arrays.asList("apple", "banana", "cherry");
// 使用String.join()方法将列表转换为字符串
String str = String.join(", ", list);
// 输出结果
System.out.println(str); // 输出: apple, banana, cherry
8、列表去重的四种方式
使用Set接口进行去重,但是需要注意的是这样去重会打乱原来List中数据的顺序。
List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 4, 5, 5, 6);
Set<Integer> set = new HashSet<>(list);
List<Integer> deduplicatedList = new ArrayList<>(set);
System.out.println(deduplicatedList); // 输出: [1, 2, 3, 4, 5, 6]
使用LinkedHashSet保留插入顺序
List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 4, 5, 5, 6);
Set<Integer> set = new LinkedHashSet<>(list);
List<Integer> deduplicatedList = new ArrayList<>(set);
System.out.println(deduplicatedList); // 输出: [1, 2, 3, 4, 5, 6],保持了插入顺序
使用stream流进行去重
List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 4, 5, 5, 6);
List<Integer> deduplicatedList = list.stream().distinct().collect(Collectors.toList());
System.out.println(deduplicatedList); // 输出: [1, 2, 3, 4, 5, 6]
手动去重
List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 4, 5, 5, 6);
List<Integer> deduplicatedList = new ArrayList<>();
Set<Integer> set = new HashSet<>();
for (Integer item : list) {
if (set.add(item)) { // 如果添加成功,说明是第一次遇到这个元素
deduplicatedList.add(item);
}
}
System.out.println(deduplicatedList); // 输出: [1, 2, 3, 4, 5, 6]
以上四种方式都可以完成对List列表的去重操作,可以根据自己的实际情况任选其一。
9、列表转数组
csList.stream().toArray(String[]::new)