Es ist ungefähr zwei Jahre her, seit ich Java geschrieben habe, und kürzlich habe ich es in Java 11 implementiert. Von Java 8 bis 9, 10 Stream und Optional Einige Methoden wurden auch zu https://docs.oracle.com/javase/jp/11/docs/api/java.base/java/util/Optional.html hinzugefügt. In der aktuellen Entwicklung haben wir Optional ernsthaft eingeführt und es ist zu einer Umgebung geworden, in der Stream und Optional effektiver genutzt werden können.
Abgesehen davon werde ich oft gefragt: "Kann ich das in Stream schreiben?" "Ich möchte hier in Stream schreiben ..." ... Als ich mir den Code anderer Leute ansah, dachte ich, dass Stream schöner geschrieben werden könnte ...
Auf jeden Fall möchte ich, dass Sie aggressiver in Stream schreiben! (Warum möchten Sie [hier] in Stream schreiben (https://qiita.com/nabekou29/items/360506a8c4ced0bfdba0))
Übrigens, wenn Sie die offizielle Referenz ohne zu zögern lesen können, ist dieser Artikel möglicherweise nicht nützlich.
--Dieser Artikel basiert auf den Java 11-Spezifikationen.
"⭐️" wird an die Elemente angehängt, die am häufigsten verwendet werden.
class Widget {
private Color color;
private int weight;
private String text;
private List<Widget> childWeights = new ArrayList<>();
public Widget(Color color, int weight) {
this.color = color;
this.weight = weight;
}
public Widget(Color color, int weight, String text) {
this(color, weight);
this.text = text;
}
public Color getColor() {
return color;
}
public int getWeight() {
return weight;
}
public Optional<String> getText() {
return Optional.ofNullable(text);
}
public List<Widget> getChildWeights() {
return childWeights;
}
public void setChildWeights(List<Widget> childWeights) {
this.childWeights = childWeights;
}
public boolean isColorRed() {
return color == RED;
}
public boolean isColorBlue() {
return color == BLUE;
}
@Override
public String toString() {
return "Widget {color: " + color + ", weigth: " + weight + "}";
}
@Override
public boolean equals(Object o) {
Widget w = (Widget) o;
return this.color == w.color && this.weight == w.weight;
}
}
Wie man Nr. 1 benutzt
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
//Aus der Liste
List<Widget> list = List.of(a, b, c); // [a, b, c]
Stream<Widget> listToStream = list.stream(); // <a, b, c>
//Vom Set
Set<Widget> set = Set.of(a, b, c); // {a, b, c}
Stream<Widget> setToStream = set.stream(); // <a, b, c>
//Von der Karte
Map<Long, Widget> map = Map.of(1L, a, 2L, b, 3L, c); // {1L : a, 2L : b, 3L : c}
Stream<Long> mapKeyToStream = map.keySet().stream(); // <1L, 2L, 3L>
Stream<Widget> mapValueToStream = map.values().stream(); // <a, b, c>
Stream<Entry<Long, Widget>> mapToStream = map.entrySet().stream(); // <{1L : a}, {2L : b}, {3L : c}>
Wenn Sie von Anfang an parallel arbeiten möchten
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
//Aus der Liste
List<Widget> list = List.of(a, b, c); // [a, b, c]
Stream<Widget> listToStream = list.parallelStream(); // <a, b, c>
//Vom Set
Set<Widget> set = Set.of(a, b, c); // {a, b, c}
Stream<Widget> setToStream = set.parallelStream(); // <a, b, c>
//Von der Karte
Map<Long, Widget> map = Map.of(1L, a, 2L, b, 3L, c); // {1L : a, 2L : b, 3L : c}
Stream<Long> mapKeyToStream = map.keySet().parallelStream(); // <1L, 2L, 3L>
Stream<Widget> mapValueToStream = map.values().parallelStream(); // <a, b, c>
Stream<Entry<Long, Widget>> mapToStream = map.entrySet().parallelStream(); // <{1L : a}, {2L : b}, {3L : c}>
Widget[] arr = {a, b, c};
Stream<Widget> arrayToStream = Arrays.stream(arr); // <a, b, c>
Stream<Long> stream1 = Stream.of(100L); // <100L>
//× Fehler
Stream<Long> stream2 = Stream.of(null); // <null> ?
Stream<Long> stream1 = Stream.of(100L, 200L); // <100L, 200L>
Stream<String> stream2 = Stream.of("A", "B", "C", null); // <"A", "B", "C", null>
Stream<Long> stream3 = Stream.of(null, null); // <null, null>
Stream<Long> stream1 = Stream.ofNullable(100L); // <100L>
Stream<Long> stream2 = Stream.ofNullable(null); // <>
Stream<Long> stream3 = Stream.empty(); // <>
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Stream<Widget> stream2 = Stream.of(x, y, z); // <x, y, z>
Stream<Widget> stream3 = Stream.concat(stream1, stream2); // <a, b, c, x, y, z>
Ich habe es nicht benutzt, aber ich kann es im Notfall so schreiben
Stream<Widget> stream1 = Stream.of(a, b); // <a, b>
Stream<Widget> stream2 = Stream.of(c, d); // <c, d>
Stream<Widget> stream3 = Stream.of(e, f); // <e, f>
Stream<Widget> stream4 = Stream.of(g, h); // <g, h>
Stream<Stream<Widget>> stream5_1 = Stream.of(stream1, stream2, stream3, stream4); // <<a, b>, <c, d>, <e, f>, <g, h>>
Stream<Widget> stream5_2 = stream5_1.flatMap(UnaryOperator.identity()); // <a, b, c, d, e, f, g, h>
IntStream stream1To4 = IntStream.range(1, 5); // IntStream<1, 2, 3, 4>
IntStream stream1To5 = IntStream.rangeClosed(1, 5); // IntStream<1, 2, 3, 4, 5>
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
// Widget -> Color
Stream<Widget> stream1_1 = Stream.of(a, b, c); // <a, b, c>
Stream<Color> stream1_2 = stream1_1.map(Widget::getColor); // <RED, RED, BLUE>
// Widget ->Gewicht(int)
Stream<Widget> stream2_1 = Stream.of(a, b, c); // <a, b, c>
Stream<Integer> stream2_2 = stream2_1.map(Widget::getWeight); // <10, 15, 20>
// number -> number * 2
IntStream stream3_1 = IntStream.range(1, 5); // IntStream<1, 2, 3, 4>
IntStream stream3_2 = stream3_1.map(i -> i * 2); // IntStream<2, 4, 6, 8>
Widget a = new Widget(RED, 10);
Widget a_1 = new Widget(RED, 1);
Widget a_2 = new Widget(RED, 2);
Widget a_3 = new Widget(RED, 4);
a.setChildWeights(List.of(a_1, a_2, a_3));
Widget b = new Widget(RED, 15);
Widget b_1 = new Widget(RED, 8);
b.setChildWeights(List.of(b_1));
Widget c = new Widget(BLUE, 20);
// Widget ->Jedes untergeordnete Widget
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Stream<List<Widget>> stream2 = stream1.map(Widget::getChildWeights); // <[a_1, a_2, a_3], [b_1], []>
Stream<Widget> stream3 = stream2.flatMap(Collection::stream); // <a_1, a_2, a_3, b_1>
Widget a = new Widget(RED, 10, "A");
Widget b = new Widget(RED, 15, "B");
Widget c = new Widget(BLUE, 20);
// Widget -> text(String)
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Stream<Optional<String>> stream2 = stream1.map(Widget::getText); // <<"A">, <"B">, <>>
Stream<String> stream3 = stream2.flatMap(Optional::stream); // <"A", "B">
Stream<Integer> stream1_1 = Stream.of(1, 2, 3, 4); // <1, 2, 3, 4>
IntStream stream1_2 = stream1_1.mapToInt(i -> i); // IntStream<1, 2, 3, 4>
IntStream stream1_1 = IntStream.range(1, 5); // IntStream<1, 2, 3, 4>
Stream<Integer> stream1_2 = stream1_1.boxed(); // <1, 2, 3, 4>
IntStream stream2_1 = IntStream.range(1, 5); // IntStream<1, 2, 3, 4>
Stream<String> stream2_2 = stream2_1.mapToObj(i -> String.valueOf(i)); // <"1", "2", "3", "4">
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Widget d = new Widget(GREEN, 25);
//Rote extrahieren
Stream<Widget> stream1_1 = Stream.of(a, b, c, d); // <a, b, c, d>
Stream<Widget> stream1_2 = stream1_1.filter(Widget::isColorRed); // <a, b>
//Extrahieren Sie diejenigen, die mehr als 10 wiegen
Stream<Widget> stream1_3 = Stream.of(a, b, c, d); // <a, b, c, d>
Stream<Widget> stream1_4 = stream1_3.filter(w -> w.getWeight() > 10); // <b, c, d>
//Nicht rote extrahieren(1)
Stream<Widget> stream2_1 = Stream.of(a, b, c, d); // <a, b, c, d>
Stream<Widget> stream2_2 = stream2_1.filter(w -> !w.isColorRed()); // <c, d>
//Nicht rote extrahieren(2)
Stream<Widget> stream2_3 = Stream.of(a, b, c, d); // <a, b, c, d>
Stream<Widget> stream2_4 = stream2_2.filter(Predicate.not(Widget::isColorRed)); // <c, d>
//Äquivalente extrahieren(1)
Stream<Widget> stream3_1 = Stream.of(a, b, c, d, null); // <a, b, c, d, null>
Stream<Widget> stream3_2= stream3_1.filter(w -> Objects.equals(w, new Widget(RED, 15))); // <b>
//Äquivalente extrahieren(2)
Stream<Widget> stream3_3 = Stream.of(a, b, c, d, null); // <a, b, c, d, null>
Stream<Widget> stream3_4= stream3_3.filter(Predicate.isEqual(new Widget(RED, 15))); // <b>
//Extrahieren Sie rote oder blaue(1)
Stream<Widget> stream4_1 = Stream.of(a, b, c, d); // <a, b, c, d>
Stream<Widget> stream4_2 = stream4_1.filter(w -> w.isColorRed() || w.isColorBlue()); // <a, b, c>
//Extrahieren Sie rote oder blaue(2)
Stream<Widget> stream4_3 = Stream.of(a, b, c, d); // <a, b, c, d>
Predicate<Widget> isColorRed = Widget::isColorRed;
// Widget::Da es keine Garantie gibt, dass isColorRed Prädikat ist
// filter((Widget::isColorRed).or(Widget::isColorBlue))Kann nicht schreiben
Stream<Widget> stream4_4 = stream4_2.filter(isColorRed.or(Widget::isColorBlue)); // <a, b, c>
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Stream<Widget> stream1_1 = Stream.of(a, a, b, b, null, null); // <a, a, b, b, null, null>
Stream<Widget> stream1_2 = stream1_1.distinct(); // <a, b, null>
Widget a = new Widget(RED, 10, "A");
Widget b = new Widget(RED, 15, "B");
Widget c = new Widget(BLUE, 20);
Widget d = new Widget(GREEN, 25;
//Nach Gewicht sortieren
Stream<Widget> stream1_1 = Stream.of(b, c, a); // <b, c, a>
Stream<Widget> stream1_2 = stream1_1.sorted(Comparator.comparing(Widget::getWeight)); // <a, b, c>
//Nach Gewicht sortieren(absteigende Reihenfolge)
Stream<Widget> stream2_1 = Stream.of(b, c, a); // <b, c, a>
Stream<Widget> stream2_2 = stream2_1.sorted(Comparator.comparing(Widget::getWeight, Comparator.reverseOrder())); // <c, b, a>
//Nach Text sortieren(Machen Sie zuerst null)
Stream<Widget> stream3_1 = Stream.of(b, c, a); // <b, c, a>
Stream<Widget> stream3_2 = stream3_1.sorted(Comparator.comparing(w -> w.getText().orElse(null), Comparator.nullsFirst(Comparator.naturalOrder()))); <c, a, b>
//Erste Sortierung:Ist es rot,Zweite Sorte:Gewicht
Stream<Widget> stream4_1 = Stream.of(b, c, a, d); // <b, c, a, d>
Stream<Widget> stream4_2 = stream4_1.sorted(Comparator.comparing(Widget::isColorRed).thenComparing(Widget::getWeight)); // <c, d, a, b>
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5); // <1, 2, 3, 4, 5>
Stream<Integer> stream2 = stream1.limit(3); // <1, 2, 3>
Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5); // <1, 2, 3, 4, 5>
Stream<Integer> stream2 = stream1.skip(2); // <3, 4, 5>
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream = Stream.of(a, b, c); // <a, b, c>
List<Widget> list = stream.collect(Collectors.toList()); // [a, b, c]
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream = Stream.of(a, b, c); // <a, b, c>
Set<Widget> set = stream.collect(Collectors.toSet()); // {a, b, c}
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Map<Integer, Color> map1 = stream.collect(Collectors.toMap(Widget::getWeight, Widget::getColor)); // {10: RED, 15: RED, 20: BLUE}
Stream<Widget> stream2_1 = Stream.of(a, b, c); // <a, b, c>
Map<Color, Widget> map2_1 = stream.collect(Collectors.toMap(Widget::getColor, UnaryOperator.identity(), (w1, w2) -> w1)); // {RED: a, BLUE: c}
Stream<Widget> stream2_2 = Stream.of(a, b, c); // <a, b, c>
Map<Color, Widget> map2_2 = stream.collect(Collectors.toMap(Widget::getColor, UnaryOperator.identity(), (w1, w2) -> w2)); // {RED: b, BLUE: c}
//× Fehler(Im Falle eines Schlüsselkonflikts muss angegeben werden, ob der erste oder der zweite priorisiert werden soll)
Stream<Widget> stream2_3 = Stream.of(a, b, c); // <a, b, c>
Map<Color, Widget> map2_3 = stream.collect(Collectors.toMap(Widget::getColor, UnaryOperator.identity()));
Stream<Widget> stream3 = Stream.of(a, b, c); // <a, b, c>
Map<Color, List<Widget>> map3 = stream3.collect(Collectors.groupingBy(Widget::getColor)); // {RED: [a, b], BLUE: [c]}
Stream<Integer> stream = Stream.of(1, 2, 3); // <1, 2, 3>
stream.forEach(System.out::println);
//Standardausgabe:
// 1
// 2
// 3
Stream<Integer> stream = Stream.of(1, 2, 3); // <1, 2, 3>
long cnt = stream.count(); // 3L
Stream<Widget> stream1 = Stream.of("A", "B", "C"); // <"A", "B", "C">
String str1 = stream1.collect(Collectors.joining()); // "ABC"
Stream<Widget> stream2 = Stream.of("A", "B", "C"); // <"A", "B", "C">
String str2 = stream2.collect(Collectors.joining(", ")); // "A, B, C"
Stream<Widget> stream3 = Stream.of("A", "B", "C"); // <"A", "B", "C">
String str3 = stream3.collect(Collectors.joining(", ", "[", "]")); // "[A, B, C]"
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream1_1 = Stream.of(a, b, c); // <a, b, c>
IntStream stream1_2 = stream1_1.mapToInt(Widget::getWeight); // <10, 15, 20>
int sum1 = stream1_2.sum(); // 45
Stream<Widget> stream2_1 = Stream.of(a, b, c); // <a, b, c>
int sum2 = stream2.collect(Collectors.summingInt(Widget::getWeight)); // 45
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Optional<Widget> widget = stream1.max(Comparator.comparing(Widget::getWeight)); // <c>
Stream<Widget> stream2_1 = Stream.of(a, b, c); // <a, b, c>
IntStream stream2_2 = stream2_1.mapToInt(Widget::getWeight); // <10, 15, 20>
OptionalInt max = stream2_2.max(); // <20>
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
Optional<Widget> widget = stream1.min(Comparator.comparing(Widget::getWeight)); // <a>
Stream<Widget> stream2_1 = Stream.of(a, b, c); // <a, b, c>
IntStream stream2_2 = stream2_1.mapToInt(Widget::getWeight); // <10, 15, 20>
OptionalInt min = stream2_2.min(); // <10>
Stream<Integer> stream = Stream.of(1, 2, 3); // <1, 2, 3>
Optional<Integer> n = stream.findFirst() // <1>
Stream<Integer> stream = Stream.of(1, 2, 3); // <1, 2, 3>
Optional<Integer> n = stream.findAny() // <1>
Widget a = new Widget(RED, 10);
Widget b = new Widget(RED, 15);
Widget c = new Widget(BLUE, 20);
Stream<Widget> stream1 = Stream.of(a, b, c); // <a, b, c>
boolean bool1 = stream1.anyMatch(Widget::isColorRed); // true
Stream<Widget> stream2 = Stream.of(c); // <c>
boolean bool2 = stream2.anyMatch(Widget::isColorRed); // false
Stream<Widget> stream3 = Stream.of(); // <>
boolean bool3 = stream3.anyMatch(Widget::isColorRed); // false
Beachten Sie das Verhalten, wenn Stream leer ist
Stream<Widget> stream1 = Stream.of(a, b); // <a, b>
boolean bool1 = stream1.allMatch(Widget::isColorRed); // true
Stream<Widget> stream2 = Stream.of(b, c); // <b, c>
boolean bool2 = stream2.allMatch(Widget::isColorRed); // false
Stream<Widget> stream3 = Stream.of(); // <>
boolean bool3 = stream3.allMatch(Widget::isColorRed); // true
Beachten Sie das Verhalten, wenn Stream leer ist
Stream<Widget> stream1 = Stream.of(c); // <c>
boolean bool1 = stream1.noneMatch(Widget::isColorRed); // true
Stream<Widget> stream2 = Stream.of(b, c); // <a, b, c>
boolean bool2 = stream2.noneMatch(Widget::isColorRed); // false
Stream<Widget> stream3 = Stream.of(); // <>
boolean bool3 = stream3.noneMatch(Widget::isColorRed); // true
Recommended Posts