Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. GROUPING BY EXAMPLE
- public Map<Integer, Set<File>> byYear() {
- return files.stream()
- .collect(Collectors.groupingBy(
- file -> file.getCreateAt().getYear(),
- Collectors.toSet()
- ));
- }
- 2. GROUPING BY EXAMPLE
- Map<String, Set<Employee>> employeesByLevels = employees.stream()
- .filter(i -> levels.contains(i.getLevel()))
- .collect(Collectors.groupingBy(
- Employee::getLevel,
- TreeMap::new, // map sorted by key
- Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Employee::getSalary,Comparator.reverseOrder()))) // values sorted too
- ));
- return employeesByLevels;
- 3. SORTING IN A MAP
- Map<String, List<Shape>> shapesByUser = shapes.stream()
- .collect(Collectors.groupingBy(
- Shape::getId,
- Collectors.toList()
- ))
- .entrySet()
- .stream()
- .sorted(Comparator
- .<Map.Entry<String, List<Shape>>>comparingInt(e -> e.getValue().size())
- .reversed()
- .thenComparingDouble(e -> e.getValue().stream().mapToDouble(Shape::getArea).sum())
- .reversed()
- )
- .collect(Collectors.toMap(
- Map.Entry::getKey,
- e -> e.getValue().stream()
- .sorted(Comparator.comparingDouble(Shape::getPerimeter).reversed())
- .collect(Collectors.toList()),
- (e1, e2) -> e1,
- LinkedHashMap::new
- ));
- 4. ATOMIC INTEGER
- AtomicInteger counter = new AtomicInteger(1);
- paymentList.stream().filter(i->i.index.equals(id))
- .sorted(Comparator.comparing(Payment::getPrice,Comparator.reverseOrder()))
- .forEach(i->printStream.println(String.format("%d. %s %d",
- counter.getAndIncrement(),i.title,i.price)));
- 5. FLATMAP
- studentsByBranch = studentsByBranch.entrySet().stream().sorted(
- Comparator.comparingLong(e->e.getValue().stream().flatMap(i->i.getGrades()
- .stream()).filter(i->i==10).count())
- ).collect(Collectors.toMap(
- Map.Entry::getKey,
- Map.Entry::getValue,
- (e1,e2)->e1,
- LinkedHashMap::new
- ));
- List<String> branches = studentsByBranch.keySet().stream().collect(Collectors.toList()).reversed();
- for(String branch : branches){
- Map<Integer,Long> numberOfStudentsPerGrade = studentsByBranch.get(branch).stream()
- .flatMap(s -> s.getGrades().stream())
- .collect(Collectors.groupingBy(
- e->e.intValue(),
- TreeMap::new,
- Collectors.counting()
- ));
- printStream.println(branch);
- numberOfStudentsPerGrade.entrySet().stream().forEach(
- i-> printStream.println(String.format("%d | (%d)",i.getKey(),i.getValue()))
- );
- }
- 6. COLLECTORS JOINING
- String[] allCourses = studentsByIndex.values().stream()
- .map(i->i.getCoursesByTerm().values().stream().flatMap(c->c.stream()).collect(Collectors.joining(",")))
- .collect(Collectors.joining(",")).split(",");
- 7. MORE MAP SORTING
- gamesByTeam.entrySet().stream()
- .sorted(Comparator.comparing(
- (Map.Entry<String, Team> e) -> e.getValue().getPoints(),
- Comparator.reverseOrder())
- .thenComparing(e -> e.getValue().getGoalDifference(),
- Comparator.reverseOrder())
- .thenComparing(e->e.getKey()))
- 8. MAP GROUPING BY 0 PROBLEM
- return IntStream.range(5, 11)
- .boxed()
- .collect(Collectors.toMap(
- grade -> grade,
- grade -> studentsByGrade.getOrDefault(grade, 0L),
- (a, b) -> a,
- TreeMap::new
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement