一 排序
1.简单排序
List<Integer> list = new ArrayList<>();
list.add(11);
list.add(200);
list.add(2);
list.add(6);
list.add(23);
Collections.sort(list); //升序
System.err.println(list);
//输出:[2, 6, 11, 23, 200]
2.整体顺序反转
Collections.reverse(list);//对list进行反转
3.当list中带有null时的几种排序
@Test
public void demo() {
List<Integer> list = new ArrayList();
list.add(1);
list.add(3);
list.add(2);
list.add(null);
Ordering<Comparable> natural = Ordering.natural();
Collections.sort(list, natural.nullsLast());
System.out.println("list = " + list);
// list = [1, 2, 3, null]
Collections.sort(list, natural.nullsFirst());
System.out.println("list = " + list);
// list = [null, 1, 2, 3]
list.removeIf(e -> Objects.isNull(e));
Collections.sort(list);
System.out.println("list = " + list);
// list = [1, 2, 3]
}
4.根据list中的某个时间字段排序
List<Map> collect = listall.stream().sorted((d1, d2) -> {
try {
SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("yyyy-MM-dd");
Date time1 = simpleDateFormatDay.parse(d1.get("createTime").toString());
Date time2 = simpleDateFormatDay.parse(d2.get("createTime").toString());
return Long.compare(time1.getTime(), time2.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}).collect(Collectors.toList());
5.根据list中的Double或Integer类型字段排序
List<Student> students = new ArrayList<>();
//student{name,age}
students.add(new Student("abc", 12.124));
students.add(new Student("bcd", 20.214));
students.add(new Student("cde", 17.321));
students.add(new Student("def", 25.145));
students.add(new Student("efg", 15.145));
//当age为Integer类型时
//students.sort((x, y) -> Integer.compare(x.getAge(), y.getAge()));
students.sort((x, y) -> Double.compare(x.getAge(), y.getAge()));//需要jdk1.8以上
6.list根据某一字段排序
List<Student> l3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList());
二 List集合进行去重
1.利用set集合的去重特性来对list集合进行去重(没有顺序)
List<Integer> list = new ArrayList();
list.add(1);
list.add(3);
list.add(2);
list.add(2);
Set<Integer> set = new HashSet<Integer>(list); //将list转换为set
list.clear(); //清空
list.addAll(new ArrayList<Integer>(set)); 将set转换回去
2.使用LinkedHashSet对list进行去重(保持原顺序)
List<Integer> list = new ArrayList();
list.add(1);
list.add(3);
list.add(2);
list.add(2);
LinkedHashSet<Integer> set = new LinkedHashSet<>(list);
System.out.println(set);
//输出:[1,3,2]
3.使用stream流对list进行去重(有序)
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(5);
list.add(3);
list.add(4);
list.add(5);
list = list.stream().distinct().collect(Collectors.toList());
//打印输出:[1,2,3,4,5]
三 List集合进行分组
1.使用stream流进行分组(根据字段)
Map<String, List<Item>> map = list.stream().collect(Collectors.groupingBy(item::getName));
2.使用stream流进行分组(根据字段)取某一元素的集合
Map<String,List<String>> typeMap = list.stream().collect(Collectors.groupingBy(map ->map.get("type"), Collectors.mapping(map1 ->map1.get("id"), Collectors.toList())));
3.根据元素分组 获取每组的条数
Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Object::getType, Collectors.summingInt(p -> 1)));
四 List合并去重
1.使用stream流进行合并去重
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
List<Integer> lists = new ArrayList<Integer>();
list.add(1);
list.add(6);
list.add(9);
List<Integer> result = Stream.of(list , lists )
.flatMap(Collection::stream).distinct().collect(Collectors.toList());
//打印输出:[1,2,3,6,9]
五 list获取元素
1.根据条件获取符合条件的元素list
List<Object> l1 = list.stream().filter(item-> item.id.equals("1")).collect(toList());
2.根据条件获取元素总数
Integer sum = list.stream().filter(item-> item.id.equals("1")).mapToInt(Object::getCount).sum();
3.求某元素的平均值
double avg = list.stream().collect(averagingInt(Object::getNum));
4.获取最大值
Object = list.stream().reduce((item, item2) -> item.getNum() > item2.getNum() ? item:item2).get();
六 list转map(list<Object>)
1.转成Map<String,Object>
Map<String,User> map = list.stream().collect(Collectors.toMap(User::getName,each->each,(value1, value2) -> value1));
2.转成Map<String,String>
Map<String,String> map = list.stream().collect(Collectors.toMap(User::getName,User::getId,(value1, value2) -> value1));
3.List 转成Map<String,List>(分组的原理)
Map<String, List<User>> map = list.stream().collect(Collectors.groupingBy(User::getName));