Przejdź do głównej zawartości

Stream peak() metoda - Java

API Note: This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline:



Stream.of("one", "two", "three", "four") .filter(e -> e.length() > 3) .peek(e -> System.out.println("Filtered value: " + e)) .map(String::toUpperCase) .peek(e -> System.out.println("Mapped value: " + e)) .collect(Collectors.toList());

Metoda pozwala wyprintować aktualny stan streamu.

Rodzaje operacji na strumieniach:

posredniczace tzw intermediate
kończące - tzw terminal
bezstanowe - np filter
stanowe - sort
redukcyjne - np max

WAZNE

  1. Strumienie są wywoływane w sposób leniwy (lazy) tzn dane sa przetwarzane w momencie wywołania metody końcowej tj terminal
  2. Transformacje nie modyfikuja wejściowych danych z których strumień został stworzony 


https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

Komentarze

Popularne posty z tego bloga

Skrócony zapis if - instrukcja warunkowa java

Instrukcja warunkowa - warunek i rezultat. if (warunek) { jesli spełniony wykonań operacje i zwróć wynik; } warunek nie spełniony Możliwości skrócenia kodu instrukcji warunkowej if (i < 0) ? i-- : i++; Jeżeli i mniejsze od zera to i-- jezeli false to i++ if (i < 0) {     i--; } else {     i++; } Skrócony zapis instrukcji warunkowej else if (i < 0) ? i--;  inna_zmienna=4; : i++; if (i < 0) {     i--; } else {     i++;     inna_zmienna = 4; } Skrócony zapis if

String-1 > withouEnd2

Given a string, return a version without both the first and last char of the string. The string may be any length, including 0. withouEnd2("Hello") → "ell" withouEnd2("abc") → "b" withouEnd2("ab") → "" public String withouEnd2 ( String str ) { if ( str . length ()== 1 ){ return str . substring ( 1 ); } else if ( str . length ()== 0 ){ return str ; } return ( str . substring ( 1 , str . length ()- 1 )); }

String-1 > middleTwo

Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string length will be at least 2. middleTwo("string") → "ri" middleTwo("code") → "od" middleTwo("Practice") → "ct" public String middleTwo ( String str ) { return str . substring (( str . length ()/ 2 - 1 ), str . length ()/ 2 + 1 ); }