forked from Apress/functional-interfaces-in-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSect9a_Ex6.java
More file actions
33 lines (32 loc) · 830 Bytes
/
Sect9a_Ex6.java
File metadata and controls
33 lines (32 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package chapter12;
import java.util.stream.*;
class Car
{
String manu;
String model;
int mpg;
public Car(String ma, String mo, int mp)
{
manu = ma;
model = mo;
mpg = mp;
}
public String toString()
{
return manu + " " + model + " gets " + mpg + " mpg";
}
}
public class Sect9a_Ex6
{
public static void main(String[] args)
{
Stream.of(new Car("Buick" ,"Regal" ,25),
new Car("Hyundai","Elantra",27),
new Car("Buick" ,"Skylark",26),
new Car("Hyundai","Accent" ,30)) // Stream<Car>
.collect(
Collectors.groupingBy(x -> x.manu))// Map<String,List<Car>>
.forEach( (x,y) ->
System.out.println(x + ": " +y ));
}
}