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.
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 thestream 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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Arrays;
import java.util.List;
public class ExampleOfStream
{
public static voidmain(String[] args)
{
List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 3);
list.stream().filter((a) -> a >3).forEach(System.out::println);
}
}
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);
}
}
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.ArrayList;
import java.util.List;
public class ExampleOfStreamFilter
{
public static voidmain(String[] args)
{
List<Student> listOfStudent = new ArrayList<Student>();
System.out.println("Class Name = "+ student.getClassName());
}
);
}
}
class Student
{
int rollNo;
String name;
String className;
public int getRollNo(){
return rollNo;
}
public voidsetRollNo(int rollNo){
this.rollNo = rollNo;
}
public String getName(){
return name;
}
public voidsetName(String name){
this.name = name;
}
public String getClassName(){
return className;
}
public voidsetClassName(String className){
this.className = className;
}
Student(String name, int rollNo, String className)
{
this.name = name;
this.rollNo = rollNo;
this.className = className;
}
}
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;
}
}
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
2. Stream.map()
This method is declared inthe 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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Arrays;
import java.util.List;
public class ExampleOfStream
{
public static voidmain(String[] args)
{
List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 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().map((a) -> a*2).forEach(System.out::println);
}
}
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.ArrayList;
import java.util.List;
public class ExampleOfStreamFilter
{
public static voidmain(String[] args)
{
List<Student> listOfStudent = new ArrayList<Student>();
Student(String name, int rollNo, String className)
{
this.name = name;
this.rollNo = rollNo;
this.className = className;
}
}
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;
}
}
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
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
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Arrays;
import java.util.List;
public class ExampleOfStreamSort
{
public static voidmain(String[] args)
{
List<Integer> list = Arrays.asList(1, 4, 2, 7, 9, 10, 3);
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);
}
}
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package First;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ExampleOfStreamFilter
{
public static voidmain(String[] args)
{
List<Student> listOfStudent = new ArrayList<Student>();
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())
return0;
elseif(student1.getRollNo()>student2.getRollNo())
return1;
else
return-1;
}
}
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;
}
}
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ExampleOfDistinct
{
public static voidmain(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
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);
}
}
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.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.ArrayList;
public class DistinctExampleUserDefined
{
public static voidmain(String[] args)
{
ArrayList<Student> listOfStudent = new ArrayList<Student>();