Details Explanation of java.util.Set enhancement in Java 9

The Set.of() static factory methods provide a convenient way to create immutable sets. The Set instances created by these methods have the following characteristics:

  • They are structurally immutable. Elements cannot be added or removed. Calling any mutator method will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the Set to behave inconsistently or its contents to appear to change.
  • They disallow null elements. Attempts to create them with null elements result in NullPointerException.
  • They are serializable if all elements are serializable.
  • They reject duplicate elements at creation time. Duplicate elements passed to a static factory method result in IllegalArgumentException.
  • The iteration order of set elements is unspecified and is subject to change.
  • They are value-based. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.
  • They are serialized as specified on the Serialized Form page.

Example:

public class SetImmutableFactoryDemo{
  public static void main(String ... args){
    java.util.Set<String> alphabet = java.util.Set.of("Java", "C", "C++");
    for(String alp:alphabet){
      System.out.println(alp);
    }
  }
}

 It will not support following things:

  1. Set.of method return immutable Set object so modification is not possible

    java.util.Set<String> alphabet = java.util.Set.of(“Java”, “C”, “C++”);

    alphabet.add(“PHP”);

    Above code will throws UnsupportedOperationException becuase Set.of method will return immutable object so once object created modification is not possible on that object.

Exception in thread "main" java.lang.UnsupportedOperationException
        at java.base/java.util.ImmutableCollections.uoe(Unknown Source)
        at java.base/java.util.ImmutableCollections$AbstractImmutableSet.add(Unknown Source)
        at Demo.main(Demo.java:4)

 

  1. Set.Of() method does not allowed duplicate element

java.util.Set<String> alphabet = java.util.Set.of(“Java”, “C”, “C++”,”C”);

Above code will throws IllegalArgumentException because as per Java9 Documentation it will throws IllegalArgumentException if any duplicate value try to add in Set using Set.Of() method.

Exception in thread "main" java.lang.IllegalArgumentException: duplicate element: C
        at java.base/java.util.ImmutableCollections$SetN.<init>(Unknown Source)
        at java.base/java.util.Set.of(Unknown Source)
        at Demo.main(Demo.java:3)

      2. set.Of() method does not allowed null element

java.util.Set<String> alphabet = java.util.Set.of(“Java”, “C”, “C++”, null);

Above code will throws NullPointerException because as per java9 Documentation Set.Of() method does not allowed null as value.

Exception in thread "main" java.lang.NullPointerException
        at java.base/java.util.ImmutableCollections$SetN.probe(Unknown Source)
        at java.base/java.util.ImmutableCollections$SetN.<init>(Unknown Source)
        at java.base/java.util.Set.of(Unknown Source)
        at Demo.main(Demo.java:3)

 

Was this post helpful?

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *