Java Collections emptySet() MethodThe emptySet() method of Java Collections class is used to get the Set that has no elements. These empty Set are immutable in nature. SyntaxFollowing is the declaration of emptySet() method: ParameterThis method does not accept any parameter. ReturnsThe emptySet() method returns an empty immutable Set. ExceptionsNA Compatibility VersionJava 1.5 and above Example 1Test it NowOutput: Empty Set: [] Example 2Test it NowOutput: Created empty immutable Set: [] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.AbstractCollection.add(AbstractCollection.java:267) at myPackage.CollectionsEmptySetExample2.main(CollectionsEmptySetExample2.java:10) Example 3Test it NowOutput: Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.AbstractCollection.add(AbstractCollection.java:267) at myPackage.CollectionsEmptySetExample3.main(CollectionsEmptySetExample3.java:8) Next TopicJava Collections Class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India