Java Collections copy() Method21 Mar 2025 | 2 min read The copy() 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. SyntaxFollowing is the declaration of copy() method: Parameter
ReturnsThe copy() method does not return anything. ExceptionsThe copy() method throws the following exceptions- IndexOutOfBoundsException- It throws an exception if the destination list is too small to store the entire source List. UnsupportedOperationException- It throws this type of exception if the destination list's list-iterator does not support the set operation. Compatibility VersionJava 1.4 and above Example 1Output: Elements of source list: [Java Tutorial, is best on, TpointTech] Elements of destination list: [Java Tutorial, is best on, TpointTech] Example 2Output: 1 2 3 4 9 10 Example 3Output: Exception in thread "main" java.lang.IndexOutOfBoundsException: Source does not fit in dest at java.base/java.util.Collections.copy(Collections.java:558) at myPackage.CollectionsCopyExample3.main(CollectionsCopyExample3.java:8) Example 4Output: Elements of Source List: [50, 10, 20] Elements of Destination List: [one, two, three, four, five] Elements of Destination List after copying Source List: [50, 10, 20, four, five] Next TopicJava Collections Class |
emptySet()
Java Collections. Method The method of Java Collections class is used to get the Set that has no elements. These empty Set are immutable in nature. Syntax Following is the declaration of method: public static final <T> Set<T> Parameter This method does not accept any parameter. Returns The ...
5 min read
checkedSet()
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 Set. Syntax Following is the declaration of method: public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) Parameter Parameter Description Required/Optional s It is the set for which...
2 min read
indexOfSubList()
Java Collections Method The method of Java Collections class is used to get the starting position of the first occurrence of the specified target list within the specified source list. It returns -1 if there is no such occurrence in the specified list. Syntax Following is...
3 min read
reverse()
Java Collections Method The method of Java Collections class is used to reverse the order of the elements in the specified list. Syntax Following is the declaration of method: public static void reverse(List<?> list) Parameter Parameter Description Required/Optional list It is the list whose elements are to be reversed. Required Returns The method does...
2 min read
addAll()
Java Collections Method The is a method of Java Collections class which adds all of the specified elements to the specified collection. The elements to be added may be specified individually or as an array. Syntax Following is the declaration of method: public static <T> boolean...
2 min read
asLifoQueue()
Java Collections Method The is an inbuilt method of Java Collections class which returns a view of a Deque as a Last-in-first-out (LIFO) Queue. In this method, the method add and remove is mapped to push and pop operation respectively. It is useful when...
2 min read
Java Collections class
Java collection class is used exclusively with static methods that operate on or return collections. It inherits Object class. The important points about are: Java Collection class supports the polymorphic algorithms that operate on collections. Java Collection class throws a NullPointerException if the collections or class objects provided...
6 min read
singletonMap()
Java Collections Method The method of Java Collections class is used to get an immutable map, mapping only the specified key to the specified value. Syntax Following is the declaration of method: public static <K,V> Map<K,V> singletonMap(K key, V value) Parameter Parameter Description Required/Optional key It is the key which will...
2 min read
nCopies()
Java Collections Method The method of Java Collections class is used to get an immutable list consisting of n copies of the specified object. Syntax Following is the declaration of method: public static <T> List<T> nCopies(int n, T o) Parameter Parameter Description Required/Optional n It is the number of elements in the...
2 min read
emptySortedSet()
Java Collections Method The method of Java Collections class is used to get the sorted Set that has no elements. These empty Set are immutable in nature. Syntax Following is the declaration of method: public static final <E> SortedSet<E> Parameter This method does not accept any parameter. Returns The...
2 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