SortedSet method stream() in Java
A SortedSet method stream() is a Collection interface method that used to create a sequential Stream with this collection as its source. It returns a sequential Stream over the elements in this collection.
Syntax
 1 default Stream<E> stream()
return : It returns a sequential Stream over the elements in this collection.
SortedSet method stream()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.SortedSet;
 6 import java.util.TreeSet;
 7 import java.util.stream.Stream;
 8 
 9 public class Main {
 10 
 11     public static void main(String[] args) {
 12 
 13         // creating immutable list
 14         List<Integer> elements = Arrays.asList(20, 10, 30, 50, 40);
 15         System.out.println("A list elements : " + elements);
 16 
 17         // create sorted set by specifying list collection
 18         SortedSet<Integer> sortedSet = new TreeSet<>(elements);
 19         System.out.println("An elements in sorted set :" + sortedSet);
 20 
 21         // create stream from SortedSet
 22         Stream<Integer> stream = sortedSet.stream();
 23         // divide each element by 2 using map() stream method
 24         stream.map(ele -> ele / 2).forEach(ele -> System.out.print(ele + " "));
 25     }
 26 }
In the above example, a SortedSet of type integer created by passing a collection that sorts elements in natural order. A stream() method called that returns Stream of Integer. A Stream method map() called that divides each element by 2 and return a resulting Stream that iterated using forEach() method that print each element value.
Output
 1 A list elements : [20, 10, 30, 50, 40]
 2 An elements in sorted set :[10, 20, 30, 40, 50]
 3 5 10 15 20 25

SortedSet method stream() with String

SortedSet method stream() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.SortedSet;
 6 import java.util.TreeSet;
 7 import java.util.stream.Stream;
 8 
 9 public class StringExp {
 10 
 11     public static void main(String[] args) {
 12 
 13         // creating immutable list
 14         List<String> elements = Arrays.asList("Welcome", "Hello", "to", "IOGyan");
 15         System.out.println("A list elements : " + elements);
 16 
 17         // create sorted set by specifying list collection
 18         SortedSet<String> sortedSet = new TreeSet<>(elements);
 19         System.out.println("An elements in sorted set :" + sortedSet);
 20 
 21         // create stream from SortedSet
 22         Stream<String> stream = sortedSet.stream();
 23         // convert each string to uppercase
 24         stream.map(String::toUpperCase).forEach(ele -> System.out.print(ele + " "));
 25     }
 26 }
In the above example, a SortedSet of type String created by passing a collection that sort elements in natural order. A stream() method called that returns Stream of Integer. A Stream method map() called that converts each element to uppercase and return a resulting Stream that iterated using forEach() method that print each string.
Output
 1 A list elements : [Welcome, Hello, to, IOGyan]
 2 An elements in sorted set :[Hello, IOGyan, Welcome, to]
 3 HELLO IOGYAN WELCOME TO

SortedSet method stream() with custom object

SortedSet method stream() with custom object
 1 package com.java;
 2 
 3 import java.util.*;
 4 import java.util.stream.Collectors;
 5 import java.util.stream.Stream;
 6 
 7 public class Example {
 8 
 9     public static void main(String[] args) {
 10 
 11         Employee emp1 = new Employee(100, "Emp1", "Tech",  12000);
 12         Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
 13         Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
 14 
 15         // creating immutable list
 16         List<Employee> elements = Arrays.asList(emp1, emp2, emp3);
 17         System.out.println("A list elements :");
 18         System.out.println(elements);
 19 
 20         // comparator that sort element in descending order
 21         Comparator<Employee> comparator = (e1, e2) -> (int) (e2.getSalary() - e1.getSalary());
 22 
 23         // create sorted set with comparator
 24         SortedSet<Employee> sortedSet = new TreeSet<>(comparator);
 25         sortedSet.addAll(elements); // adding elements into sorted set
 26         System.out.println("An elements in sorted set :" + sortedSet);
 27 
 28         // create stream from SortedSet
 29         Stream<Employee> stream = sortedSet.stream();
 30         // sum of salary based on department
 31         Map<String, Double> deptSum = stream.collect(
 32                 Collectors.groupingBy(
 33                         Employee::getDept,
 34                         Collectors.summingDouble(Employee::getSalary)));
 35         deptSum.forEach((key, value) -> {
 36             System.out.println(key + " => " + value);
 37         });
 38     }
 39 }
In the above example, a SortedSet of type Employee created by passing a lambda function that compares employee based on salary in descending order. A SortedSet method stream() called that returns Stream of Employee and compute sum of salary based on department and return Map. A Map entry iterated using forEach() that prints department name and sum of salary.
Output
 1 A list elements :
 2 [Emp1 Tech 12000, Emp2 Tech 31000, Emp3 Admin 21000]
 3 An elements in sorted set :[Emp2 Tech 31000, Emp3 Admin 21000, Emp1 Tech 12000]
 4 Tech => 43000.0
 5 Admin => 21000.0

Employee class

Employee class
 1 package com.java;
 2 
 3 public class Employee {
 4 
 5     private final int id;
 6     private final String name;
 7     private final String dept;
 8     private final Long salary;
 9 
 10     public Employee(int id, String name, String dept, long salary) {
 11         this.id = id;
 12         this.name = name;
 13         this.dept = dept;
 14         this.salary = salary;
 15     }
 16     public int getId() {
 17         return this.id;
 18     }
 19 
 20     public String getDept() {
 21         return this.dept;
 22     }
 23 
 24     public String getName() {
 25         return this.name;
 26     }
 27 
 28     public Long getSalary() {
 29         return this.salary;
 30     }
 31 
 32     @Override
 33     public int hashCode() {
 34         return this.getId() + this.getName().hashCode();
 35     }
 36 
 37     @Override
 38     public boolean equals(Object obj) {
 39         return this.id == ((Employee)obj).id;
 40     }
 41 
 42     public String toString() {
 43         return this.name + " " + this.dept + " " + this.salary;
 44     }
 45 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us