Java Stream API cheat sheet

Stream super convenient. Stream Justice, and Stream forget soon. So I made a cheat sheet. Thank you for your consideration.

Process everything (List)

Arrays.asList("honto", "world", "tough").stream().forEach(s -> System.out.println(s));
Arrays.asList("Seriously", "really", "tough").stream().forEach(System.out::println);

Process everything (Map)

Collections.emptyMap().entrySet()
  .forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));

Process only specific items.

Arrays.asList("love", "Love", "Money", "dream").stream()
  .filter(s -> s.equals("Love")).forEach(System.out::println);

Type conversion.

Arrays.asList("1", "2", "3", "5").stream()
  .map(s -> Integer.valueOf(s)).forEach(System.out::println);

Process for a specific range of numbers.

IntStream.range(0, 10).map(i -> i*2).toArray();

Change from List to Map.

Arrays.asList("SU-METAL DEATH!", "YUIMETAL DEATH!", "MOAMETAL DEATH!").stream()
  .map(s -> s.split(" ",2)).filter(a -> 2 == a.length)
  .collect(Collectors.toMap(a->a[0], a->a[1]));

Process at high speed with multiple threads.

Arrays.asList("love", "Love", "Money", "dream").parallelStream()
  .findFirst(s -> s.equals("Love"));

List join

Arrays.asList("Love,that is,Sweet","Love,that is,strongly","Love,that is,Noble").stream()
  .map(s -> s.split(",")).flatMap(a -> Arrays.asList(a).stream())
  .collect(Collectors.toList());

Variable when it gets longer.

Function<String, String> toHakata = s -> {
    return s + "Toyo.";
};
Arrays.asList("Sogyan Kotsunaka").stream().map(toHakata);

If there are others

Please comment and pull request.

Recommended Posts

Java Stream API cheat sheet
Java Stream API
[Java] Stream API / map
Java8 Stream API practice
Java Stream API in 5 minutes
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Introduction to Stream API
[Java] Stream API intermediate operation
C # cheat sheet for Java engineers
javac, jar, java command cheat sheet
I tried using Java8 Stream API
Java 8 ~ Stream API ~ to start now
Getting Started with Doma-Criteria API Cheat Sheet
Stream API memo
Data processing using stream API from Java 8
[JAVA] Stream type
JMeter cheat sheet
Try using the Stream API in Java
Try Java 8 Stream
Nowadays Java lambda expressions and Stream API
[Java] Data type / string class cheat sheet
Try various Java Stream API methods (now)
Studying Java 8 (Stream)
Kotlin cheat sheet
Java Stream termination
[Docker cheat sheet]
[Java] Stream processing
Stream API basics
Java 9 Optional :: stream
[Java] How to operate List using Stream API
Mockito + PowerMock cheat sheet
[Java] Stream Collectors notes
Pack API response (java)
Eclipse Collections cheat sheet
Rails Tutorial cheat sheet
[Java] Stream API-Stream generation
Stream API (Collectors class)
Stream API map method
Docker-Client Java API Troubleshooting
Spring Boot2 cheat sheet
SCSS notation cheat sheet
Zabbix API in Java
Java8 Stream reduction operation
Oreshiki docker-compose cheat sheet
Docker command cheat sheet
Java8 Stream Rough Summary
[For beginners] How to operate Stream API after Java 8
Use Java lambda expressions outside of the Stream API
[Java11] Stream Summary -Advantages of Stream-
[Must-see for apprentice java engineer] How to use Stream API
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Java8 stream, lambda expression summary
[Eclipse] Shortcut key cheat sheet
Java Stream cannot be reused.
Use Redis Stream in Java
[Java11] Stream Usage Summary -Basics-
Convert 2D array to csv format with Java 8 Stream API
Java Basic Learning Content 8 (Java API)
Java application for beginners: stream
Recent Java API specification generation