Java Stream Collectors.toUnmodifiableSet()

By Arvind Rai, October 03, 2020
On this page we will provide Java Stream Collectors.toUnmodifiableSet() example introduced in Java 10.
1. The Collectors.toUnmodifiableSet() returns a Collector that accumulates the input elements into an unmodifiable Set.
2. The returned Collector throws NullPointerException for null values.
3. If the input contains duplicate elements, an arbitrary element of the duplicates is preserved.
4. This Collector is unordered.
5. Find the method declaration from Java doc.
public static <T> Collector<T,​?,​Set<T>> toUnmodifiableSet() 

Example-1

In this example we will create unmodifiable set of integer.
Example1.java
package com.concretepage;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Example1 {
  public static void main(String[] args) {
	Set<Integer> set1 = Stream.of(15, 25, 25, 35, 35)
		.collect(Collectors.toUnmodifiableSet());
	System.out.println(set1);

	Set<Integer> set2 = IntStream.range(20,  25).boxed()
		.collect(Collectors.toUnmodifiableSet());
	System.out.println(set2);
	
	Set<Integer> set3 = Arrays.asList(20, 30, 40, 40).stream().map(i -> i * 2)
		.collect(Collectors.toUnmodifiableSet());
	System.out.println(set3);	
  }
} 
Output
[35, 25, 15]
[22, 23, 24, 20, 21]
[40, 60, 80] 
When we try to modify unmodifiable set as given below,
set1.add(40); 
Exception will be thrown.
Exception in thread "main" java.lang.UnsupportedOperationException 

Example-2

In this example we will create unmodifiable set of string.
Example2.java
package com.concretepage;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example2 {
  public static void main(String[] args) {
	Set<String> set1 = Stream.of("Suresh", "Lakshman", "Bharat")
		.collect(Collectors.toUnmodifiableSet());
	System.out.println(set1);
	
	Set<String> set2 = Stream.of("Mahesh", "Krishn", "Ram").map(s -> "Sri " + s)
		.collect(Collectors.toUnmodifiableSet());
	System.out.println(set2);	
  }
} 
Output
[Suresh, Bharat, Lakshman]
[Sri Ram, Sri Mahesh, Sri Krishn] 

Example-3

In this example we will create unmodifiable set of objects.
Example3.java
package com.concretepage;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example3 {
  public static void main(String[] args) {
     Set<User> set = Stream.of(new User(101, "Mahesh"), new User(102, "Krishna"))
        .collect(Collectors.toUnmodifiableSet());
     System.out.println(set);
  }
}
class User {
	private int id;
	private String name;
	public User(int id, String name) {
	  this.id = id;
	  this.name = name;
	}
        //Sets and Gets        

	@Override
	public String toString() {
	  return id + "-" + name;
	}
} 
Output
[102-Krishna, 101-Mahesh] 

Reference

Stream Collectors
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us