转载:https://www.cnblogs.com/yzhmj/p/14923416.html?ivk_sa=1024320u

List

基础类型 List 排序

  1. List<Integer> list = Arrays.asList(3,1,5,8,6,10);
  2. log.info("未排序的list:{}",list);
  3. // 升序
  4. list.sort(Comparator.naturalOrder());
  5. log.info("升序的list:{}",list);
  6. // 降序
  7. list.sort(Comparator.reverseOrder());
  8. log.info("降序的list:{}",list);

结果
集合类 - 图1

对 List 的某个属性排序

  1. Student student1 = new Student("张三",19,175);
  2. Student student2 = new Student("李四",18,165);
  3. Student student3 = new Student("王五",20,170);
  4. List<Student> studentList = Lists.newArrayList();
  5. studentList.add(student1);
  6. studentList.add(student2);
  7. studentList.add(student3);
  8. log.info("未排序的list:{}",studentList);
  9. //对年龄降序排序
  10. List<Student> orderAgeDescList = studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());
  11. log.info("对年龄降序排序后:{}",orderAgeDescList);
  12. //对年龄升序排序
  13. List<Student> orderAgeAscList = studentList.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());
  14. log.info("对年龄升序排序后:{}",orderAgeAscList);
  15. //对年龄降序排序,对身高升序排序
  16. List<Student> orderAgeDescAndHeightAscList = studentList.stream().sorted(Comparator.comparing(Student::getAge).reversed().thenComparing(Student::getHeight)).collect(Collectors.toList());
  17. log.info("对年龄降序排序,对身高升序排序后:{}",orderAgeDescAndHeightAscList);

结果
集合类 - 图2
注意:当有多个属性排序时,先满足前一个排序,再对后一个排序

最大值、最小值、平均值、总数

  1. // 最大值
  2. Double max = doubleList.stream().max(Comparator.comparingDouble(Double::doubleValue)).orElse(null);
  3. Double min = doubleList.stream().min(Comparator.comparingDouble(Double::doubleValue)).orElse(null);
  4. // 最小值
  5. Double max = doubleList.stream().mapToDouble(Double::doubleValue).max().getAsDouble();
  6. Double min = doubleList.stream().mapToDouble(Double::doubleValue).min().getAsDouble();
  7. // 平均值
  8. Double avg = doubleList.stream().mapToDouble(Double::doubleValue).average().getAsDouble();
  9. // 总数
  10. Double sum = doubleList.stream().mapToDouble(Double::doubleValue).sum();

去重

  1. @Test
  2. public void myTest1(){
  3. List<Role> list = Lists.newArrayList();
  4. list.add(new Role(1,"张三"));
  5. list.add(new Role(2,"李四"));
  6. list.add(new Role(1,"王五"));
  7. log.info("list去重前:{}",list);
  8. //根据roleId去重
  9. list = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(Role::getRoleId))), ArrayList::new));
  10. log.info("list根据roleId去重之后:{}",list);
  11. }

结果
集合类 - 图3

List、String、String[]的互相转换

  1. // String转List
  2. // 方式一
  3. String ids= "1,2,3,4,5,6";
  4. List<String> result = Arrays.asList(ids.split(","));
  5. // 方式二
  6. List<String> result = Arrays.asList(ids.split("[,.]"));
  7. // 方式三
  8. String[] result = ids.split(",");
  9. List<String> list = Arrays.asList(result);
  10. // List转String
  11. // 方式一
  12. List<String> result = new ArrayList<>();
  13. result.add("a");
  14. result.add("b");
  15. result.add("c");
  16. String ids = String.join(",", result);
  17. // 方式二
  18. List<String> idsList = Arrays.asList("1", "2", "3","4", "5", "6");
  19. String ids = idsList.stream().collect(Collectors.joining(","));
  20. // List转String[]
  21. String[] array = result.toArray(new String[]{});

将一组数据平均分成 n 组

  1. /**
  2. * 将一组数据平均分成n组
  3. *
  4. * @param source 要分组的数据源
  5. * @param n 平均分成n组
  6. * @param <T>
  7. * @return
  8. */
  9. public static <T> List<List<T>> averageAssign(List<T> source, int n) {
  10. List<List<T>> result = new ArrayList<>();
  11. int remainder = source.size() % n; // (先计算出余数)
  12. int number = source.size() / n; // 然后是商
  13. int offset = 0; // 偏移量
  14. for (int i = 0; i < n; i++) {
  15. List<T> value;
  16. if (remainder > 0) {
  17. value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
  18. remainder--;
  19. offset++;
  20. } else {
  21. value = source.subList(i * number + offset, (i + 1) * number + offset);
  22. }
  23. result.add(value);
  24. }
  25. return result;
  26. }

将一组数据固定分组,每组 n 个元素

  1. /**
  2. * 将一组数据固定分组,每组n个元素
  3. * @param source 要分组的数据源
  4. * @param n 每组n个元素
  5. * @param <T>
  6. * @return
  7. */
  8. public static <T> List<List<T>> fixedGrouping(List<T> source, int n) {
  9. if (null == source || source.size() == 0 || n <= 0)
  10. return null;
  11. List<List<T>> result = new ArrayList<>();
  12. int sourceSize = source.size();
  13. int size = (source.size() / n) + 1;
  14. for (int i = 0; i < size; i++) {
  15. List<T> subset = new ArrayList<T>();
  16. for (int j = i * n; j < (i + 1) * n; j++) {
  17. if (j < sourceSize) {
  18. subset.add(source.get(j));
  19. }
  20. }
  21. result.add(subset);
  22. }
  23. return result;
  24. }

Map

转载:https://www.cnblogs.com/yzhmj/p/14919027.html

对 Map 的 key 或 value 进行排序(升序、降序)

  1. // 先定义 Map
  2. Map<Integer,Integer> map = Maps.newHashMap();
  3. map.put(2,11);
  4. map.put(1,33);
  5. map.put(3,22);

按 key 降序排序

  1. //注意:此处初始化map时,若定义为Maps.newHashMap();会使排序失效。因HashMap本身结构是不具备排序的
  2. Map<Integer,Integer> descOrderKeyMap = Maps.newLinkedHashMap();
  3. map.entrySet().stream().sorted(Map.Entry.<Integer,Integer>comparingByKey().reversed()).forEachOrdered(e->descOrderKeyMap.put(e.getKey(),e.getValue()));
  4. log.info("按key降序排序:{}",descOrderKeyMap);

按 key 升序排序

  1. Map<Integer,Integer> ascOrderKeyMap = Maps.newLinkedHashMap();
  2. map.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(e->ascOrderKeyMap.put(e.getKey(),e.getValue()));
  3. log.info("按key升序排序:{}",ascOrderKeyMap);

按 value 降序排序

  1. Map<Integer,Integer> descOrderValueMap = Maps.newLinkedHashMap();
  2. map.entrySet().stream().sorted(Map.Entry.<Integer,Integer>comparingByValue().reversed()).forEachOrdered(e->descOrderValueMap.put(e.getKey(),e.getValue()));
  3. log.info("按value降序排序:{}",descOrderValueMap);

按 value 升序排序

  1. Map<Integer,Integer> ascOrderValueMap = Maps.newLinkedHashMap();
  2. map.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered(e->ascOrderValueMap.put(e.getKey(),e.getValue()));
  3. log.info("按value升序排序:{}",ascOrderValueMap);

结果如下
集合类 - 图4