LinkedList addAll() Method in Java
-
java.util.LinkedList.addAll(Collection C): This method is used to append all of the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator.
Syntax:
boolean addAll(Collection C)
Parameters: The parameter C is a collection of ArrayList. It is the collection whose elements are needed to be appended at the end of the list.
Return Value: The method returns true if at least one action of append is performed.
Below program illustrate the Java.util.LinkedList.addAll() method:
// Java code to illustrate boolean addAll()importjava.util.*;importjava.util.LinkedList;importjava.util.ArrayList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]) {// Creating an empty LinkedListLinkedList<String> list =newLinkedList<String>();// Use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// A collection is createdCollection<String> collect =newArrayList<String>();collect.add("A");collect.add("Computer");collect.add("Portal");collect.add("for");collect.add("Geeks");// Displaying the listSystem.out.println("The LinkedList is: "+ list);// Appending the collection to the listlist.addAll(collect);// Clearing the list using clear() and displayingSystem.out.println("The new linked list is: "+ list);}}chevron_rightfilter_noneOutput:The LinkedList is: [Geeks, for, Geeks, 10, 20] The new linked list is: [Geeks, for, Geeks, 10, 20, A, Computer, Portal, for, Geeks]
-
java.util.LinkedList.addAll(int index, Collection C): This method is used to append all of the elements from the collection passed as parameter to this function at a specific index or position of a list.
Syntax:
boolean addAll(int index, Collection C)
Parameters: This function accepts two parameters as shown in the above syntax and are described below.
- index: This parameter is of integer datatype and specifies the position in the list starting from where the elements from the container will be inserted.
- C: It is a collection of ArrayList. It is the collection whose elements are needed to be appended.
Return Value: The method returns TRUE if at least one action of append is performed.
Below program illustrate the Java.util.LinkedList.addAll() method:
// Java code to illustrate boolean addAll()importjava.util.*;importjava.util.LinkedList;importjava.util.ArrayList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]) {// Creating an empty LinkedListLinkedList<String> list =newLinkedList<String>();// Use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Creating a CollectionCollection<String> collect =newArrayList<String>();collect.add("A");collect.add("Computer");collect.add("Portal");collect.add("for");collect.add("Geeks");// Displaying the listSystem.out.println("The LinkedList is: "+ list);// Appending the collection to the listlist.addAll(1, collect);// Clearing the list using clear() and displayingSystem.out.println("The new linked list is: "+ list);}}chevron_rightfilter_noneOutput:The LinkedList is: [Geeks, for, Geeks, 10, 20] The new linked list is: [Geeks, A, Computer, Portal, for, Geeks, for, Geeks, 10, 20]
Recommended Posts:
- ArrayDeque addAll() method in Java
- Vector addAll() Method in Java
- Set addAll() method in Java with Examples
- ConcurrentLinkedQueue addAll() method in Java
- TreeSet addAll() Method in Java
- NavigableSet addAll() method in Java
- AbstractCollection addAll() Method in Java with Examples
- AbstractSequentialList addAll() Method in Java with Examples
- LinkedBlockingDeque addAll() method in Java with Examples
- AbstractList addAll() method in Java with Examples
- List addAll() Method in Java with Examples
- CopyOnWriteArraySet addAll() method in Java with Examples
- CopyOnWriteArrayList addAll() method in Java with Examples
- Stack addAll(Collection) method in Java with Example
- Stack addAll(int, Collection) method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



