HashSet method parallelStream() in Java
A HashSet method parallelStream() used to create parallel Stream with this collection as its source, if possible. It returns a possibly parallel Stream over the elements in this collection.
Syntax
 1 default Stream<E> parallelStream()
return : It returns a possibly parallel Stream over the elements in this collection.
HashSet method parallelStream()
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashSet;
 5 import java.util.List;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list
 12         List<Integer> list = Arrays.asList(20, 10, 30, 40);
 13         HashSet<Integer> hashSet = new HashSet<>(list);
 14         System.out.println("A hash set elements : " + hashSet);
 15 
 16         // create parallel stream
 17         hashSet.parallelStream().map(ele -> ele / 2)
 18                 .forEach(ele -> System.out.print(ele + ", "));
 19     }
 20 }
In the above example, a HashSet of type integer created by passing a list. A parallelStream() called that returns parallel stream over the elements. A map() method of Stream called by passing a mapper that divide elements by 2 and returns Stream object with resulting value. A resulting Stream element iterated using forEach() and print element separated by comma.
Output
 1 A hash set elements : [20, 40, 10, 30]
 2 15, 20, 10, 5,

HashSet method parallelStream() with String

HashSet method parallelStream() with String
 1 package iogyan.example;
 2 
 3 import java.util.Arrays;
 4 import java.util.HashSet;
 5 import java.util.List;
 6 
 7 public class StringMain {
 8 
 9     public static void main(String[] args) {
 10 
 11         // create immutable list
 12         List<String> list = Arrays.asList("Welcome", "to", "IOGyan");
 13         HashSet<String> hashSet = new HashSet<>(list);
 14         System.out.println("A hash set elements : " + hashSet);
 15 
 16         // create parallel stream
 17         hashSet.parallelStream().map(String::toUpperCase)
 18                 .forEach(ele -> System.out.print(ele + ", "));
 19     }
 20 }
In the above example, a HashSet of type string created by passing a list. A parallelStream() called that returns parallel stream over the elements. A map() method of Stream called by passing a mapper that converts string to uppercase and returns Stream object with resulting value. A resulting Stream element iterated using forEach() and print element separated by comma.
Output
 1 A hash set elements : [IOGyan, Welcome, to]
 2 IOGYAN, WELCOME, TO,

HashSet method parallelStream() with custom object

HashSet method parallelStream() with custom object
 1 package iogyan.example;
 2 
 3 import iogyan.model.Employee;
 4 
 5 import java.util.Arrays;
 6 import java.util.HashSet;
 7 import java.util.List;
 8 
 9 public class EmployeeMain {
 10 
 11     public static void main(String[] args) {
 12 
 13         Employee emp1 = new Employee(1, "Emp1","Tech", 1000);
 14         Employee emp2 = new Employee(2, "Emp2","Admin", 5000);
 15         Employee emp3 = new Employee(3, "Emp3","Admin", 12000);
 16 
 17         // create immutable list
 18         List<Employee> list = Arrays.asList(emp1, emp3, emp2);
 19         HashSet<Employee> hashSet = new HashSet<>(list);
 20         System.out.println("A hash set elements : " + hashSet);
 21 
 22         // create parallel stream
 23         hashSet.parallelStream().map(Employee::getSalary)
 24                 .forEach(ele -> System.out.print(ele + ", "));
 25     }
 26 }
In the above example, a HashSet of type Employee created by passing a list. A parallelStream() called that returns parallel stream over the elements. A map() method of Stream called by passing a mapper that converts employee to double by using Employee class getSalary() method and returns Stream object with resulting value. A resulting Stream element iterated using forEach() and print element separated by comma.
Output
 1 A hash set elements : [Emp3 Admin 12000.0, Emp1 Tech 1000.0, Emp2 Admin 5000.0]
 2 1000.0, 5000.0, 12000.0,

Employee class

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