Intermediate operation in Java 8

Here is the table content of the article will we will cover this topic.
1. What are the intermediate operation in Java 8?
2. Stream.filter()
3. Stream.map()
4.  Stream.sorted()
5. Stream .distinct()

What are the intermediate operations in Java 8?

In this article, we will discuss the Intermediate operation in Java 8. By use of Stream, you can perform various operations. Some operations that return a new stream is called intermediate operation. This operation is lazy in nature.

1. By use of intermediate operation, you can perform the various operations in a row.
Because intermediate operation produces a stream and sends next to operation.
2. The intermediate operations could not able to produce the final result.
3. Intermediate operation in java 8, is used by the terminal operation as an input.

Let’s discuss some intermediate operations:
1. Stream.filter()
2. Stream.map()
3.  Stream.sorted()
4. Stream .distinct()

1. Stream.filter()

This method is declared in the Stream interface. It accepts a predicate as a parameter. The predicate is used to filter all elements of the stream. This is an intermediate operation of the stream and produces the new stream. Because it just filters the elements from the stream. By use of the returned stream, we can apply terminal operation(forEach()) and produce the result.

Suppose we have an ArrayList of numbers. We want to print only those numbers which are greater than 3.

import java.util.Arrays;
import java.util.List;
public class ExampleOfStream 
{
    public static void main(String[] args) 
    {
    	List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 3);
    	list.stream().filter((a) -> a > 3).forEach(System.out::println);
    }	
}

Output: 4
7
9
10

Let’s take another example of a User-defined class. Suppose we have a list of Students and we want to print the detail of those students having roll numbers less than 104.

import java.util.ArrayList;
import java.util.List;
public class ExampleOfStreamFilter 
{
    public static void main(String[] args) 
    {
    	List<Student> listOfStudent = new ArrayList<Student>();
    	listOfStudent.add(new Student("Ram", 101, "MCA"));
    	listOfStudent.add(new Student("Sham", 102, "MCA"));
    	listOfStudent.add(new Student("Krishna", 103, "MCA"));
    	listOfStudent.add(new Student("Radha", 104, "MCA"));
    	listOfStudent.add(new Student("Shri", 105, "MCA"));
    	
    	listOfStudent.stream().
    			filter((student) -> student.getRollNo() < 104).
    			forEach((student) ->
    			{
    				System.out.println("RollNo = "+ student.getRollNo());
    				System.out.println("Name = "+ student.getName());
    				System.out.println("Class Name = "+ student.getClassName());
    			}
    			);
    }	
}


class Student
{
	int rollNo;
	String name;
	String className;
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	
	Student(String name, int rollNo, String className)
	{
		this.name = name;
		this.rollNo = rollNo;
		this.className = className;
	}
}

Output: RollNo = 101
Name = Ram
Class Name = MCA
RollNo = 102
Name = Sham
Class Name = MCA
RollNo = 103
Name = Krishna
Class Name = MCA

Intermediate operation in Java 8

2. Stream.map()

This method is declared in the Stream interface. It accepts a function as a parameter. The function is used to apply to each element of the stream. This is the intermediate operation of the stream and produces the new stream. Because it only performs the operation on each element of the stream. By use of the returned stream, we can apply terminal operation(forEach()) and produce the result.

Suppose we have an ArrayList of numbers. We want to multiple each ArrayList by 2.

import java.util.Arrays;
import java.util.List;
public class ExampleOfStream 
{
    public static void main(String[] args) 
    {
    	List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 3);
    	list.stream().map((a) -> a*2).forEach(System.out::println);
    }	
}

Output: 2
8
4
14
18
20
6

Let’s take another example of a User-defined class. Suppose we have a list of Students and we want to print the name of the student in capital letters.

import java.util.ArrayList;
import java.util.List;
public class ExampleOfStreamFilter 
{
    public static void main(String[] args) 
    {
    	List<Student> listOfStudent = new ArrayList<Student>();
    	listOfStudent.add(new Student("Ram", 101, "MCA"));
    	listOfStudent.add(new Student("Sham", 102, "MCA"));
    	listOfStudent.add(new Student("Krishna", 103, "MCA"));
    	listOfStudent.add(new Student("Radha", 104, "MCA"));
    	listOfStudent.add(new Student("Shri", 105, "MCA"));
    	
    	listOfStudent.stream().
    			map((student) -> student.getName().toUpperCase()).
    			forEach((name) -> System.out.println(name));
    			
    			
    }	
}


class Student
{
	int rollNo;
	String name;
	String className;
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	
	Student(String name, int rollNo, String className)
	{
		this.name = name;
		this.rollNo = rollNo;
		this.className = className;
	}
}

Output: RAM
SHAM
KRISHNA
RADHA
SHRI

Intermediate operation in Java 8

3. Stream.Sorted()

This method is declared in the Stream interface. It accepts a Comparator as a parameter. If you do not provide any comparator, the elements are sorted in the natural order. This is the intermediate operation of the stream and produces the new stream. Because it returns a sorted view of the stream. By use of the returned stream, we can apply terminal operation(forEach()) and produce the result.
NOTE: The sorted() method creates a sorted view of the stream. It doesn’t manipulate the ordering of the collection at backed.

Suppose we have an ArrayList of numbers. We want to sort them

import java.util.Arrays;
import java.util.List;
public class ExampleOfStreamSort 
{
    public static void main(String[] args) 
    {
    	List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 3);
    	list.stream().sorted().forEach(System.out::println);
    }	
}

Output: 1
2
3
4
7
9
10

Let’s take another example of a User-defined class with Comparator. Suppose we have a list of Students and we want to print the name of the student in capital letters.

package First;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ExampleOfStreamFilter 
{
    public static void main(String[] args) 
    {
    	List<Student> listOfStudent = new ArrayList<Student>();
    	listOfStudent.add(new Student("Ram", 101, "MCA"));
    	listOfStudent.add(new Student("Sham", 102, "MCA"));
    	listOfStudent.add(new Student("Krishna", 103, "MCA"));
    	listOfStudent.add(new Student("Radha", 104, "MCA"));
    	listOfStudent.add(new Student("Shri", 105, "MCA"));
    	
    	listOfStudent.stream().sorted(new RollNoComparator()).forEach((student ->
    	{
    		System.out.println(student.getRollNo());
    	}));
    			
    			
    }	
}


class Student
{
	int rollNo;
	String name;
	String className;
	public int getRollNo() {
		return rollNo;
	}
	public void setRollNo(int rollNo) {
		this.rollNo = rollNo;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	
	Student(String name, int rollNo, String className)
	{
		this.name = name;
		this.rollNo = rollNo;
		this.className = className;
	}
}

class RollNoComparator implements Comparator<Student>
{  
  @Override
  public int compare(Student student1, Student student2) 
  {
    if(student1.getRollNo()==student2.getRollNo())  
    return 0;  
    else if(student1.getRollNo()>student2.getRollNo())  
    return 1;  
    else  
    return -1;  
  }  
}  

Output: 101
102
103
104
105

4. Stream distinct()

This method is declared in the Stream interface. It doesn’t accept any parameters. It returns a stream consisting of distinct elements. This method works based on the equals method of the class. It makes the comparison with each element and returns only distinct elements. This is the intermediate operation of a stream and produces the new stream. By use of the returned stream, we can apply terminal operation(forEach()) and produce the result.

Suppose we have an ArrayList that contains duplicate numbers. We want to get a stream with distinct values.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExampleOfDistinct 
{
	public static void main(String[] args) 
	{
		List<String> names = new ArrayList<String>();
		names.add("Ram");
		names.add("Sham");
		names.add("Krishna");
		names.add("Ram");
		names.add("Sham");
		
		// Printing only distinct names from list of String 
		System.out.println("Names from List:");
		names.stream().distinct().forEach(System.out::println);
		
		
		List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 3, 1, 7, 2);
		
		// Printing only distinct numbers from list of number 
		System.out.println("Numbers from List:");
		numbers.stream().distinct().forEach(System.out::println);
	}
}

Output: Names from List:
Ram
Sham
Krishna
Numbers from List:
1
2
3
4
7

Let’s take another example of a User-defined class. Suppose we have a list of Students and there are Student’s data that are duplicated. We want to print the detail of the student without duplicity.

import java.util.ArrayList;

public class DistinctExampleUserDefined 
{
	public static void main(String[] args) 
	{
		ArrayList<Student> listOfStudent = new ArrayList<Student>();
		listOfStudent.add(new Student(101, "MCA", "Ram"));
		listOfStudent.add(new Student(102, "MCA", "Sham"));
		listOfStudent.add(new Student(103, "MCA", "Krishna"));
		listOfStudent.add(new Student(104, "MCA", "Satyam"));
		listOfStudent.add(new Student(105, "MCA", "Joshi"));
		listOfStudent.add(new Student(105, "MCA", "Ram"));
		listOfStudent.add(new Student(103, "MCA", "Krishna"));
		
		listOfStudent.stream().distinct().forEach((student -> 
		{
			System.out.println("Student RollNo:"+ student.getRollNo());
			System.out.println("Student Name:"+student.getName());
			System.out.println("Student Class:"+student.getClassName());
		}));
	}

}


class Student 
{
	int rollNo;
	String className;
	String name;
	
	public Student(int rollNo, String className, String name) 
	{
		this.rollNo = rollNo;
		this.className = className;
		this.name = name;
	}
	public int getRollNo() 
	{
		return rollNo;
	}
	public void setRollNo(int rollNo) 
	{
		this.rollNo = rollNo;
	}
	public String getClassName() 
	{
		return className;
	}
	public void setClassName(String className) 
	{
		this.className = className;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public boolean equals(Object obj) 
	{
		return this.rollNo == ((Student)obj).getRollNo();
	}
	@Override
	public int hashCode() 
	{
		return this.rollNo;
	}
}

Output: Student RollNo:101
Student Name:Ram
Student Class:MCA
Student RollNo:102
Student Name:Sham
Student Class:MCA
Student RollNo:103
Student Name:Krishna
Student Class:MCA
Student RollNo:104
Student Name:Satyam
Student Class:MCA
Student RollNo:105
Student Name:Joshi
Student Class:MCA

Leave a Comment