sneppets-java8

How to manipulate Java 8 Stream data sources or objects?

This tutorial shows you how to manipulate Java 8 stream data sources or objects.

Manipulate Java 8 Stream data sources

The below example shows you how to modify Java 8 streams data source or objects without interference with data source during the execution of stream pipeline.

package com.sneppets.example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.plan.domain.Skill;

public class ManipulateStreamObjectsExample {
	
	public static void main (String[] args) {
		
		//first a list is created consisting of three skills
		List<Skill> skillList = new ArrayList<>();		
		skillList.add(new Skill(1L, "Java"));		
		skillList.add(new Skill(2L, "Python"));		
		skillList.add(new Skill(3L, "Golang"));
		
		//Then a stream is created from the above list
		Stream<Skill> skills = skillList.stream();
		
		//Next the list is modified by adding fourth skill
		skillList.add(new Skill(4L, "C"));
		
		//Finally the elements of the stream are collected 
		List<Skill> modifiedList = skills.collect(Collectors.toList());
		
		System.out.println("Skills After Manipulation");
		print(modifiedList);
	}

	private static void print(List<Skill> modifiedList) {
		
		for(Skill skill : modifiedList) {
			
			System.out.println(skill.getName());
		}
	}

}

Skill.java

public class Skill {
	
	private String name;
        
        private Long id;
	
	public Skill() {
		
	}
	
	public Skill(Long id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

        public Long getId() {
                return id;
        }

        public void setId(Long id) {
                this.id = id;
        }


}

Output

Skills After Manipulation
Java
Python
Golang
C

“Streams enable you to execute possibly-parallel aggregate operations over a variety of data sources, including even non-thread-safe collections such as ArrayList. This is possible only if we can prevent interference with the data source during the execution of a stream pipeline.

For most data sources, preventing interference means ensuring that the data source is not modified at all during the execution of the stream pipeline. For well-behaved stream sources, the source can be modified before the terminal operation”.

Note the above lines from Java 8 Stream documentation. And follow the below steps to modify stream’s data source.

Create List First

First a list is created consisting of three skills

List<Skill> skillList = new ArrayList<>();		
skillList.add(new Skill(1L, "Java"));		
skillList.add(new Skill(2L, "Python"));		
skillList.add(new Skill(3L, "Golang"));

Stream creation

Then a stream is created from the above list

Stream<Skill> skills = skillList.stream();

Modify the data source

Next the list is modified by adding fourth skill “C”

skillList.add(new Skill(4L, "C"));

Terminal Operation – collect()

Finally the elements of the stream are collected. Note, for well-behaved stream sources, the source must be modified before the terminal collect() operation as shown in this example.

List<Skill> modifiedList = skills.collect(Collectors.toList());

Also See

References

guest
0 Comments
Inline Feedbacks
View all comments