java stream A memorandum of intermediate / termination operations that are really often used 1

In the previous post, I wrote the basics of Stream, the flow of intermediate operation → termination operation. It feels like a memorandum, but I will write down the ones I have used so far and the intermediate and termination operations that seem to be used frequently.

First of all, from map () and Collectors.toList (), which I personally think are the most frequently used. For example, suppose you have a list of Staff classes with name, id, etc.


public class Staff {

    private String name;
    private Long id;
    private int age;
・
・
・
//getter/setter etc.
・
・

If you want a list that extracts only the ID for processing reasons, assuming that the ID contains numbers from 1 in sequence (no duplication) It can be extracted by using map () and Collectors.toList () as follows.

    //staffList has 6 elements
 
        List<Long> idList = staffList.stream().map(staff -> staff.getId()).collect(Collectors.toList());
        idList.stream().forEach((id) -> {
            System.out.println(id.toString());
        });

Output result

1
2
3
4
5
6
7
8
9
10
11
12

The processing performed above is map(staff -> staff.getId()) In the part of, staff, which is one element extracted from staffList, is converted to staff.getId (). afterwards, collect(Collectors.toList()) List the staff.getId () group extracted from the staff of all elements in. As a result, a list with only IDs extracted has been obtained. (We have 12 staff members using IntStream, but I will explain IntStream someday ... I will post only the code this time)

//12 staff members prepared by IntStream, if even 1 millimeter is helpful somewhere
List<Staff> staffList = IntStream.rangeClosed(1, 12).boxed()
				.map(i -> new Staff(String.valueOf(i) + "You", Long.valueOf(i), i)).collect(Collectors.toList());

The map (conversion) used here can be used in various places, so don't forget it.

The next thing that seems to come out most often is filter. map was a conversion, but filter is a search. Here, the name of the staff is requested by specifying the ID. For the name of each staff member, I entered the ID number with you added.

//Argument searchID= 1L
        Staff target = staffList.stream().filter(staff -> staff.getId() == searchID).findFirst().orElse(null);
        System.out.println(target.getName());

Output result

1 you

filter(staff -> staff.getId() == searchID) Specify the conditions to squeeze with the above filter method.

findFirst().orElse(null) First, the Staff that matches the filter is acquired, and if it is not found, null is returned.

There are many other things that I often use, so I will find time to add them.

Recommended Posts

java stream A memorandum of intermediate / termination operations that are really often used 1
A concise summary of Java 8 date / time APIs that are likely to be used frequently
A collection of RSpecs that I used frequently
A description of Interface that is often mistaken
Java Stream termination
[Gachi consideration] About abbreviations of English words that are often used when naming variables
[Java] When writing the source ... A memorandum of understanding ①
A collection of commands that were frequently used on heroku