Java Collections emptyList() MethodThe emptyList() method of Java Collections class is used to get a List that has no elements. These empty list are immutable in nature. SyntaxFollowing is the declaration of emptyList() method: ParameterThis method does not accept any parameter. ReturnsThe emptyList() method returns an empty immutable list. ExceptionsNA Compatibility VersionJava 1.5 and above Example 1Test it NowOutput: Empty list: [] Example 2Test it NowOutput: Created empty immutable list: [] Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.AbstractList.add(AbstractList.java:153) at java.base/java.util.AbstractList.add(AbstractList.java:111) at myPackage.CollectionsEmptyListExample1.main(CollectionsEmptyListExample1.java:9) Example 3Test it NowOutput: Exception in thread "main" java.lang.UnsupportedOperationException at java.base/java.util.AbstractList.add(AbstractList.java:153) at java.base/java.util.AbstractList.add(AbstractList.java:111) at myPackage.CollectionsEmptyListExample3.main(CollectionsEmptyListExample3.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