import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest {
private static final List<Apple> APPLES = create();
private static List<Apple> create() {
Apple a = new Apple(1, Apple.Color.RED, 200);
Apple a2 = new Apple(2, Apple.Color.GREEN, 250);
Apple a3 = new Apple(3, Apple.Color.RED, 260);
Apple a4 = new Apple(4, Apple.Color.GREEN, 230);
return List.of(a, a2, a3, a4);
}
public static void main(String[] args) {
}
private static void forEach() {
for (Apple a : APPLES) {
System.out.println(a.getWeight());
}
APPLES.forEach(a -> System.out.println(a.getWeight()));
}
private static List<Apple> getRedApples(Apple.Color c) {
List<Apple> reds = new ArrayList<>();
for (Apple a : APPLES) {
if (c == a.getColor()) {
reds.add(a);
}
}
return reds;
}
private static List<Apple> filter(Apple.Color c) {
return APPLES.stream()
.filter(a -> a.getColor() == c)
.collect(Collectors.toList());
}
private static void filter(Apple.Color c, int weight) {
APPLES.stream()
.filter(a -> a.getColor() == c)
.filter(a -> a.getWeight() >= weight)
.collect(Collectors.toList())
.forEach(a -> System.out.println(a.getId()));
}
private static void map() {
APPLES.stream()
.map(Apple::getWeight)
.collect(Collectors.toList())
.forEach(System.out::println);
}
private static void sorted() {
APPLES.stream()
.sorted(Comparator.comparing(Apple::getWeight))
.collect(Collectors.toList())
.forEach(a -> System.out.println(a.getWeight()));
}
private static void sortedReversed() {
APPLES.stream()
.sorted(Comparator.comparing(Apple::getId).reversed())
.collect(Collectors.toList())
.forEach(a -> System.out.println(a.getId()));
}
private static void collect() {
APPLES.stream()
.map(Apple::getColor)
.collect(Collectors.toList())
.forEach(System.out::println);
}
private static void collectGroupingBy() {
Map<Apple.Color, List<Apple>> map = APPLES.stream()
.collect(Collectors.groupingBy(Apple::getColor));
}
private static void collectGroupingBy2() {
APPLES.stream()
.collect(Collectors.toMap(Apple::getId, a -> a))
.forEach((k, v) -> {
System.out.println(k + "/" + v.getColor());
});
}
private static void finalValue() {
int count = 0;
APPLES.forEach(a -> {
System.out.println(count);
});
}
private static void getFilter() {
Apple a1 = new Apple(1, Apple.Color.RED, 200);
Apple a2 = new Apple(5, Apple.Color.GREEN, 240);
List<Apple> newApples = List.of(a1, a2);
List<Apple> oldApples = APPLES;
List<Apple> apples = new ArrayList<>();
for (Apple a : newApples) {
for (Apple oa : oldApples) {
if (a.getId() == oa.getId()) {
apples.add(a);
}
}
}
apples.forEach(a -> System.out.println(a.getId()));
System.out.println("--------------");
List<Apple> apples2 = newApples.stream()
.filter(a -> oldApples.stream()
.anyMatch(oa -> oa.getId() == a.getId()))
.collect(Collectors.toList());
apples2.forEach(a -> System.out.println(a.getId()));
}
}
package stream;
public class Apple {
public enum Color{
RED, GREEN
}
private int id;
private Color color;
private int weight;
public Apple(int id, Color color, int weight) {
this.id = id;
this.color = color;
this.weight = weight;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}