Memo Stream

Purpose

Know roughly what you can do with stream. I'm not scared to see it for the time being. I try to call it somehow.

code

Uninflected word

		//For example, make a list like this
		List<String> list = new ArrayList<String>();
		list.add("sample");
		list.add("sample1");
		list.add("sample2");
		list.add("sample3");
		list.add("sample4");
		list.add("sample5");

		list
		.stream() //Start processing the target collection
		.filter(s -> s.length() == 6) //Intermediate operation (processing)
		.forEach(s -> System.out.println(s)); //Termination operation (last processing)
		
		 //Output only sample

In this way, using the stream () method of collection, perform some intermediate operations and finally perform the termination operation to form it.

Basically

stream () is used in List, but Stream exists as an interface just because List has a method. So

Stream<String> st = list.stream();
st.forEach(s -> System.out.println(s));

You can also do something like.

Termination operation

forEach Note that it is a void method

list.stream().forEach(s -> sampleClass.stringList.add(s));
sampleClass.stringList.stream().forEach(s -> System.out.println(s));

collect Use collect () because List is not returned even if you play with stream ()

List<String>editList = list.stream().filter(s -> s.length() == 6).collect(Collectors.toList());
		editList.stream().forEach(s -> System.out.println(s));

There are other operations such as count () that returns the number of elements

Use intermediate operations

After that, just insert various intermediate operations List items that are likely to be used frequently

		.stream()
		.distinct() //Duplicate deletion
		.filter(s -> !s.isEmpty()) // isEmpty()Not only leave
		.sorted() //Simply ascending
		.sorted(Comparator.reverseOrder()) //Simply descending
		.map(changeToLength) //Use the created Function
		.forEach(s -> System.out.println(s));

The created Function is as follows

		//Create a Function called changeToLength with String as an argument and Integer as a return value
		//For String s{}Perform the processing inside.
		Function<String, Integer> changeToLength= (String s) -> {
			return s.length();
		};

Recommended Posts

Memo Stream
Stream API memo
Integer memo
docker memo
Lombok memo
Dockerfile memo
Iterator memo
corretto memo
Stream play
Java memo
AWS memo
Stream termination
Dcokerfile memo
Ruby memo
docker tutorial (memo)
java anything memo
primary key memo
Docker operation memo
Eclipse trick memo
Java Silver memo
[JAVA] Stream type
java, maven memo
Try Java 8 Stream
JavaParser usage memo
Docker command memo
Java Stream API
Stream generation process
SpringBoot-JPA-Hibernate addictive memo
Java SE 7 memo
[Memo] docker summary
OpenJDK 11 installation memo
MySQL migration memo (1)
WatchService usage memo
PlantUML usage memo
java anything memo 2
Docker-compose command memo
Studying Java 8 (Stream)
e-Gov setting memo
[wip] Ruby memo
Maven3 error memo
Stream intermediate processing
Java Stream termination
Java specification memo
[Java] Stream processing
JUnit5 usage memo
Stream API basics
Java 9 Optional :: stream
Java pattern memo