File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .winterbe .java8 ;
2+
3+ import java .util .Arrays ;
4+ import java .util .List ;
5+ import java .util .Map ;
6+ import java .util .stream .Collectors ;
7+
8+ /**
9+ * @author Benjamin Winterberg
10+ */
11+ public class Streams10 {
12+
13+ static class Person {
14+ String name ;
15+ int age ;
16+
17+ Person (String name , int age ) {
18+ this .name = name ;
19+ this .age = age ;
20+ }
21+
22+ @ Override
23+ public String toString () {
24+ return name ;
25+ }
26+ }
27+
28+ public static void main (String [] args ) {
29+ test1 ();
30+ }
31+
32+ private static void test1 () {
33+ List <Person > persons =
34+ Arrays .asList (
35+ new Person ("Max" , 18 ),
36+ new Person ("Peter" , 23 ),
37+ new Person ("Brenda" , 23 ),
38+ new Person ("David" , 12 ));
39+
40+ Map <Integer , List <Person >> personsByAge = persons
41+ .stream ()
42+ .collect (Collectors .groupingBy (p -> p .age ));
43+
44+ personsByAge
45+ .forEach ((age , p ) -> System .out .format ("age: %s; persons: %s\n " , age , p ));
46+ }
47+
48+ }
You can’t perform that action at this time.
0 commit comments