Java Collections singleton() Method21 Mar 2025 | 2 min read The singleton() method of Java Collections class is used to get an immutable set which contains only the specified object. SyntaxFollowing is the declaration of singleton() method: Parameter
ReturnsThe singleton() method returns an immutable set which contains only the specified object. ExceptionsNA Compatibility VersionJava 1.3 and above Example 1Output: [1, 2, 4, 2, 7, 2, 4, 4] [1, 2, 4, 2, 7, 2, 4] [1, 2, 2, 7, 2] Example 2Output: [A, B, C, A, C, D, C] [A, C, A, C, D, C] [A, A, D] Example 3Output: Enter the value: A Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at myPackage.CollectionsSingletonExample3.main(CollectionsSingletonExample3.java:7) Next TopicJava Collections Class |
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) sorted set backed by the specified sorted set. Syntax Following is the declaration of method: public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) Parameter Parameter Description Required/Optional s It is the sorted set which will be wrapped in...
3 min read
Java Collections Method The method of Java Collections class is used to get an immutable list which contains only the specified object. Syntax Following is the declaration of method: public static <T> List<T> singletonList(T o) Parameter Parameter Description Required/Optional o It is the object which will be stored in the returned List. Required Returns The...
2 min read
Java Collections Method The is a method of Java Collections class which returns a dynamically typesafe view of the specified Queue. If an element inserted of the wrong type, it will result in an immediate ClassCastException. Syntax Following is the declaration of method: public static <E>...
2 min read
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) collection backed by the specified collection. Syntax Following is the declaration of method: public static <T> Collection<T> synchronizedCollection(Collection<T> c) Parameter Parameter Description Required/Optional c It is the collection to be "wrapped" in a synchronized collection. Required Returns The ...
2 min read
Java Collections Method The method of Java Collections class is used to get a synchronized (thread-safe) navigable set backed by the specified navigable set. Syntax Following is the declaration of method: public static <T> NavigableSet<T> synchronizedNavigableSet(NavigableSet<T> s) Parameter Parameter Description Required/Optional s It is the navigable set which will be wrapped in...
3 min read
Java Collections Method The is an inbuilt method of Java Collections class. This method is used to get a dynamically typesafe view of the specified Navigable Set. If any attempt of inserting an element whose value has the wrong type, it will through an...
2 min read
Java Collections Method The method of Java Collections class is used to get an unmodifiable view of the specified collection. If any attempt occurs to modify the returned collection whether direct or via its iterator, result in an UnsupportedOperationException. Syntax Following is the declaration of ...
2 min read
Java Collections Method The method of Java Collections class copies all of the elements from one list into another list. In this method, the size of the destination list must be greater than or equal to the size of the source list. Syntax Following is the...
3 min read
Java Collections Method The is a Java Collections class method which returns a comparator that imposes the reverse of the natural ordering on the objects. There is two different types of Java method which can be differentiated depending on its parameter. These are: Java...
3 min read
The sort() method of Java Collections class is used to sort the elements presents in the specified list. There is two different types of Java sort() method which can be differentiated depending on its parameter. These are: Java Collections sort(list) Method Java Collections sort(list, comp) Method Java Collections...
3 min read
We request you to subscribe our newsletter for upcoming updates.
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