实体类
public class Student { private int id; private String name; private String score; private int classNo; public Student(int id, String name, String score, int classNo) { this.id = id; this.name = name; this.score = score; this.classNo = classNo; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getScore() { return score; } public void setScore(String score) { this.score = score; } public int getClassNo() { return classNo; } public void setClassNo(int classNo) { this.classNo = classNo; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", score='" + score + '\'' + ", classNo=" + classNo + '}'; } }
Main
import com.demo.entity.Student; import java.util.*; import java.util.stream.Collectors; public class Main { private static List<String> simpleList = new ArrayList<>(); private static List<Student> normalList = new ArrayList<>(); static { simpleList.add("apple"); simpleList.add("apple"); simpleList.add("banana"); simpleList.add("orange"); normalList.add(new Student(1, "Emma", "A", 701)); normalList.add(new Student(2, "Larissa", "S", 701)); normalList.add(new Student(3, "Sophia", "B", 701)); normalList.add(new Student(4, "Ashley", "B", 702)); normalList.add(new Student(5, "May", "C", 702)); normalList.add(new Student(6, "Hailey", "D", 702)); normalList.add(new Student(7, "Kelly", "S", 703)); normalList.add(new Student(8, "Amy", "A", 703)); normalList.add(new Student(9, "Wesley", "C", 703)); } public static void main(String[] args){ // TODO } }
转换
System.out.println("----------------简单List---------------"); simpleList.forEach(System.out::println); System.out.println("----------------简单List转Set---------------"); Set<String> simpleSet = new HashSet<>(simpleList); simpleSet.forEach(System.out::println);
输出:
----------------简单List--------------- apple apple banana orange ----------------简单List转Set--------------- banana orange apple
---
System.out.println("----------------普通List---------------"); normalList.forEach(System.out::println); System.out.println("----------------普通List转Set---------------"); Set<Integer> normalSet = normalList.stream().map(Student::getClassNo).collect(Collectors.toSet()); normalSet.forEach(System.out::println);
输出:
----------------普通List--------------- Student{id=1, name='Emma', score='A', classNo=701} Student{id=2, name='Larissa', score='S', classNo=701} Student{id=3, name='Sophia', score='B', classNo=701} Student{id=4, name='Ashley', score='B', classNo=702} Student{id=5, name='May', score='C', classNo=702} Student{id=6, name='Hailey', score='D', classNo=702} Student{id=7, name='Kelly', score='S', classNo=703} Student{id=8, name='Amy', score='A', classNo=703} Student{id=9, name='Wesley', score='C', classNo=703} ----------------普通List转Set--------------- 701 702 703
---
System.out.println("----------------普通List转List---------------"); List<String> list = normalList.stream().map(Student::getName).collect(Collectors.toList()); list.forEach(System.out::println);
输出:
----------------普通List转List---------------
Emma
Larissa
Sophia
Ashley
May
Hailey
Kelly
Amy
Wesley
---
System.out.println("----------------普通List转Map---------------"); Map<Integer,Student> normalMap = normalList.stream().collect(Collectors.toMap(Student::getId,(b)->b)); normalMap.forEach((id, student) -> { System.out.println(id + "::" + student); });
输出:
----------------普通List转Map--------------- 1::Student{id=1, name='Emma', score='A', classNo=701} 2::Student{id=2, name='Larissa', score='S', classNo=701} 3::Student{id=3, name='Sophia', score='B', classNo=701} 4::Student{id=4, name='Ashley', score='B', classNo=702} 5::Student{id=5, name='May', score='C', classNo=702} 6::Student{id=6, name='Hailey', score='D', classNo=702} 7::Student{id=7, name='Kelly', score='S', classNo=703} 8::Student{id=8, name='Amy', score='A', classNo=703} 9::Student{id=9, name='Wesley', score='C', classNo=703}
复杂一点的转换:
List<Student> students = new ArrayList<>(normalList); System.out.println("----------------原数据---------------"); students.forEach(System.out::println); System.out.println("----------------List<Student>转Map<String, Student>重复key只保留前者---------------"); // 重复key处理 (s1, s2) -> s1) Map<Integer, Student> classStudentMap = students.stream().collect(Collectors.toMap(Student::getClassNo, s -> s, (s1, s2) -> s1)); classStudentMap.forEach((classNo, student) -> System.out.println(classNo + "::" + student)); System.out.println("----------------List<Student>转Map<String, List<Student>>---------------"); // 重复key处理成一个集合 Map<Integer, List<Student>> listMap = students.stream().collect(Collectors.toMap(Student::getClassNo, s -> { List<Student> l = new ArrayList<>(); l.add(s); return l; }, (List<Student> s1, List<Student> s2) -> { s1.addAll(s2); return s1; })); listMap.forEach((learn, student) -> System.out.println(learn + "::" + student));
输出:
----------------原数据--------------- Student{id=1, name='Emma', score='A', classNo=701} Student{id=2, name='Larissa', score='S', classNo=701} Student{id=3, name='Sophia', score='B', classNo=701} Student{id=4, name='Ashley', score='B', classNo=702} Student{id=5, name='May', score='C', classNo=702} Student{id=6, name='Hailey', score='D', classNo=702} Student{id=7, name='Kelly', score='S', classNo=703} Student{id=8, name='Amy', score='A', classNo=703} Student{id=9, name='Wesley', score='C', classNo=703} ----------------List<Student>转Map<String, Student>重复key只保留前者--------------- 701::Student{id=1, name='Emma', score='A', classNo=701} 702::Student{id=4, name='Ashley', score='B', classNo=702} 703::Student{id=7, name='Kelly', score='S', classNo=703} ----------------List<Student>转Map<String, List<Student>>--------------- 701::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=3, name='Sophia', score='B', classNo=701}] 702::[Student{id=4, name='Ashley', score='B', classNo=702}, Student{id=5, name='May', score='C', classNo=702}, Student{id=6, name='Hailey', score='D', classNo=702}] 703::[Student{id=7, name='Kelly', score='S', classNo=703}, Student{id=8, name='Amy', score='A', classNo=703}, Student{id=9, name='Wesley', score='C', classNo=703}]
---
List<Student> students = new ArrayList<>(normalList); // 先分组,准备好数据 Map<String, List<Student>> grouping = students.stream().collect(Collectors.groupingBy(Student::getScore)); System.out.println("----------------Map<String, List<Student>>转List<Student>---------------"); // 把map的values全部拆出来 List<Student> collect = grouping.entrySet().stream() .flatMap(map -> map.getValue().stream()) .collect(Collectors.toList()); collect.forEach(System.out::println); System.out.println("----------------Map<String, List<Student>>转List<List<Student>>---------------"); // 只要map的value,但是不改变格式 grouping.values().forEach(System.out::println);
输出:
----------------Map<String, List<Student>>转List<Student>--------------- Student{id=1, name='Emma', score='A', classNo=701} Student{id=8, name='Amy', score='A', classNo=703} Student{id=3, name='Sophia', score='B', classNo=701} Student{id=4, name='Ashley', score='B', classNo=702} Student{id=5, name='May', score='C', classNo=702} Student{id=9, name='Wesley', score='C', classNo=703} Student{id=2, name='Larissa', score='S', classNo=701} Student{id=7, name='Kelly', score='S', classNo=703} Student{id=6, name='Hailey', score='D', classNo=702} ----------------Map<String, List<Student>>转List<List<Student>>--------------- [Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] [Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] [Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] [Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] [Student{id=6, name='Hailey', score='D', classNo=702}]
---
分组
List<Student> students = new ArrayList<>(normalList); System.out.println("----------------分组---------------"); // 根据key分组 Map<String, List<Student>> grouping = students.stream().collect(Collectors.groupingBy(Student::getScore)); grouping.forEach((score, student) -> System.out.println(score + "::" + student)); System.out.println("----------------按照多个属性分组---------------"); // 根据多个key的组合分组 grouping = students.stream().collect(Collectors.groupingBy( e -> e.getClassNo() + "_" + e.getScore())); grouping.forEach((learn, student) -> System.out.println(learn + "::" + student));
输出:
----------------分组--------------- A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] ----------------按照多个属性分组--------------- 702_C::[Student{id=5, name='May', score='C', classNo=702}] 703_A::[Student{id=8, name='Amy', score='A', classNo=703}] 702_B::[Student{id=4, name='Ashley', score='B', classNo=702}] 701_S::[Student{id=2, name='Larissa', score='S', classNo=701}] 703_C::[Student{id=9, name='Wesley', score='C', classNo=703}] 703_S::[Student{id=7, name='Kelly', score='S', classNo=703}] 702_D::[Student{id=6, name='Hailey', score='D', classNo=702}] 701_B::[Student{id=3, name='Sophia', score='B', classNo=701}] 701_A::[Student{id=1, name='Emma', score='A', classNo=701}]
---
排序
根据名字排序
System.out.println("----------------List排序---------------"); // 虽然这里是浅拷贝,但是只影响修改而不影响排序 List<Student> students = new ArrayList<>(normalList); Collections.sort(students, Comparator.comparing(Student::getName)); // 比上面更简洁 // students.sort(Comparator.comparing(Student::getName)); students.forEach(System.out::println);
输出:
----------------List排序--------------- Student{id=8, name='Amy', score='A', classNo=703} Student{id=4, name='Ashley', score='B', classNo=702} Student{id=1, name='Emma', score='A', classNo=701} Student{id=6, name='Hailey', score='D', classNo=702} Student{id=7, name='Kelly', score='S', classNo=703} Student{id=2, name='Larissa', score='S', classNo=701} Student{id=5, name='May', score='C', classNo=702} Student{id=3, name='Sophia', score='B', classNo=701} Student{id=9, name='Wesley', score='C', classNo=703}
---
System.out.println("----------------简单List排序---------------"); List<String> list = new ArrayList<>(simpleList); System.out.println("----------------正序---------------"); list.sort((a,b) -> a.compareTo(b)); // 更简洁的方式 // list.sort(Comparator.naturalOrder()); list.forEach(System.out::println); System.out.println("----------------倒序---------------"); list.sort(Comparator.reverseOrder()); list.forEach(System.out::println);
输出:
----------------简单List排序--------------- ----------------正序--------------- apple apple banana orange ----------------倒序--------------- orange banana apple apple
Map排序
List<Student> students = new ArrayList<>(normalList); // 先按照成绩分组,准备好数据 Map<String, List<Student>> grouping = students.stream().collect(Collectors.groupingBy(Student::getScore)); System.out.println("----------------Map<String, List<Student>>排序---------------"); Map<String, List<Student>> result = new LinkedHashMap<>(); // Map的key有特殊处理 grouping.entrySet().stream() .sorted((o1,o2) -> { Integer k1 = getWeight(o1.getKey()); Integer k2 = getWeight(o2.getKey()); return k1.compareTo(k2); }) .forEachOrdered(x -> result.put(x.getKey(), x.getValue())); result.forEach((learn, student) -> System.out.println(learn + "::" + student)); System.out.println("----------------"); Map<String, List<Student>> result2 = new LinkedHashMap<>(); // 仅仅按照key排序 grouping.entrySet().stream() .sorted(Map.Entry.comparingByKey()) .forEachOrdered(x -> result2.put(x.getKey(), x.getValue())); result2.forEach((learn, student) -> System.out.println(learn + "::" + student)); System.out.println("----------------"); Map<String, List<Student>> result3 = new LinkedHashMap<>(); // 等价第一个,只是省去了getKey方法 grouping.entrySet().stream() .sorted(Map.Entry.comparingByKey((o1,o2) -> { Integer k1 = getWeight(o1); Integer k2 = getWeight(o2); return k1.compareTo(k2); })) .forEachOrdered(x -> result3.put(x.getKey(), x.getValue())); result3.forEach((learn, student) -> System.out.println(learn + "::" + student));
权重方法:
/** * 不同成绩有不同的排序权重 * @param score * @return */ public static Integer getWeight(String score){ switch (score){ case "S": return 1; case "A": return 2; case "B": return 3; case "C": return 2; case "D": return 2; default:return 0; } }
输出:
----------------Map<String, List<Student>>排序--------------- S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] ---------------- A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] ---------------- S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}]
如果你要倒序的话
System.out.println("----------------倒序----------------"); Map<String, List<Student>> result4 = new LinkedHashMap<>(); // 仅仅按照key排序 grouping.entrySet().stream() .sorted(Map.Entry.<String, List<Student>>comparingByKey().reversed()) .forEachOrdered(x -> result4.put(x.getKey(), x.getValue())); result4.forEach((learn, student) -> System.out.println(learn + "::" + student)); System.out.println("----------------"); Map<String, List<Student>> result5 = new LinkedHashMap<>(); // 等价第一个,只是省去了getKey方法 grouping.entrySet().stream() .sorted(Map.Entry.<String, List<Student>>comparingByKey((o1,o2) -> { Integer k1 = getWeight(o1); Integer k2 = getWeight(o2); return k1.compareTo(k2); }).reversed()) .forEachOrdered(x -> result5.put(x.getKey(), x.getValue())); result5.forEach((learn, student) -> System.out.println(learn + "::" + student));
输出:
----------------倒序---------------- S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] ---------------- B::[Student{id=3, name='Sophia', score='B', classNo=701}, Student{id=4, name='Ashley', score='B', classNo=702}] A::[Student{id=1, name='Emma', score='A', classNo=701}, Student{id=8, name='Amy', score='A', classNo=703}] C::[Student{id=5, name='May', score='C', classNo=702}, Student{id=9, name='Wesley', score='C', classNo=703}] D::[Student{id=6, name='Hailey', score='D', classNo=702}] S::[Student{id=2, name='Larissa', score='S', classNo=701}, Student{id=7, name='Kelly', score='S', classNo=703}]
随时补充