This article is intended for people who can't read the Java Stream API.
Recommended for the following people
--I used to write Java, but I haven't written it recently --I quit coding because I couldn't read the Stream API ――I just want to know the point because I don't have time anyway
The Java Stream API has nothing to do with I / O classes such as InputStream and OutputStream. A mechanism that processes a collection object such as List or Set, or a loopable element such as an array only once in a loop.
--Convert entity (map method) --Filter the collection (filter method)
Here is the Java code.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List<String> csvList = new ArrayList<>();
csvList.add("Naoki Tanaka,MAN");
csvList.add("Kazuki Kitamura,MAN");
csvList.add("Shizuka Kato,WOMAN");
csvList.add("Ono no Imoko,UNKNOWN");
// 1.Convert an entity.(map)
List<Person> persons = csvList.stream().map(Person::convertPersonFromCSV).collect(Collectors.toList());
// 2.Filter entities.(filter)
List<Person> mens = persons.stream().filter(p -> p.getGender() == Gender.MAN).collect(Collectors.toList());
System.out.println("Print all persons");
persons.forEach(System.out::println);
System.out.println("Print mens only");
mens.forEach(p -> System.out.println(p.getName()));
}
enum Gender {
MAN,
WOMAN,
UNKNOWN;
}
static class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
@Override
public String toString() {
return String.format("My name is %s . \r\nMy gender is %s.", name, gender);
}
public String getName() {
return this.name;
}
public Gender getGender() {
return this.gender;
}
public static Person convertPersonFromCSV(String csv) {
String[] csvStrings = csv.split(",");
if (csvStrings.length != 2) throw new IllegalArgumentException("csv is illegal format.");
return new Person(csvStrings[0], Gender.valueOf(csvStrings[1]));
}
}
}
Let's look at the above in order
When converting an entity, use the map method. Use the map method to convert, and the collect method to convert Stream to Collection type again. </ b>
There are three main ways to write:
csvList.stream().map(Person::convertPersonFromCSV).collect(Collectors.toList());
csvList.stream().map(csv -> Person.convertPersonFromCSV(csv)).collect(Collectors.toList());
csvList.stream().map(csv -> {
Person person = Person.convertPersonFromCSV(csv);
return person;
}).collect(Collectors.toList());
Everything is the same, get the String that is an element of csvList, I am executing a method that converts it to a Person object and returns it.
--The first is how to write by omitting the csv argument --The second is how to write in one line, omitting the return method ――The third is how to write without omitting anything
By using Stream, I was able to convert List
You can create a new collection object by excluding certain elements from the collection. </ b> Use the filter method to describe the object you want to keep.
persons.stream().filter(p -> p.getGender() == Gender.MAN).collect(Collectors.toList());
By using Stream, I was able to create a new collection with only men extracted from List
Recommended Posts