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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package com.example;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
public class StreamTest {
/**
* Desk
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Desktop {
private String type;
private String color;
private int width;
private int height;
// ...
// ...
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Dimension {
private String customString;
private int width;
private int height;
public int area() {
return width * height;
}
}
public static void main(String[] args) {
var desks = Arrays.asList(new Desktop("Desk1", "white", 100, 100),
new Desktop("Desk2", "white", 110, 110),
new Desktop("Desk3", "black", 120, 120),
new Desktop("Desk4", "black", 130, 130),
new Desktop("Desk5", "black", 140, 140));
var maxArea = desks.stream().map(d -> new Dimension(d.getType(), d.getWidth(), d.getHeight()))
.max(Comparator.comparingInt(Dimension::area));
System.out.println(maxArea.get()); // StreamTest.Dimension(customString=Desk5, width=140, height=140)
var deskArrayList = Arrays.asList(new Desktop[] {
new Desktop("Desk1", "white", 100, 100),
new Desktop("Desk2", "white", 110, 110)
},
new Desktop[] {
new Desktop("Desk3", "black", 120, 120),
new Desktop("Desk4", "black", 130, 130),
},
new Desktop[] {
new Desktop("Desk5", "black", 140, 140)
});
maxArea = deskArrayList.stream()
.flatMap(l -> Stream.of(l).map(d -> new Dimension(d.getType(), d.getWidth(), d.getHeight())))
.max(Comparator.comparingInt(Dimension::area));
System.out.println(maxArea.get()); // StreamTest.Dimension(customString=Desk5, width=140, height=140)
}
}
|