LinkedList addAll(Collection) in Java

addAll(Collection): This method is available in java.util.LinkedList class of Java.

Syntax:

boolean java.util.LinkedList.addAll(Collection<? extends K> c)

This method takes one argument. This method appends all of the elements in the specified collection to the end of this list.

Parameters: One parameter is required for this method.

c: a collection containing elements to be added to this list.

Returns: true if this list changed as a result of the call.

Throws:

NullPointerException - if the specified collection is null

Approach 1: When no exception

Java

import java.util.LinkedList;

public class LinkedListaddAll {
    public static void main(String[] args) {

        LinkedList<Integer> linkedList = new LinkedList<>();

        linkedList.add(1);
        linkedList.add(2);
        LinkedList<Integer> linkedList2 = new LinkedList<>();

        linkedList2.addAll(linkedList);

        System.out.println(linkedList2);
    }
}

Output:

[1, 2]


Approach 2: NullPointerException 

Java

import java.util.LinkedList;

public class LinkedListaddAll {
    public static void main(String[] args) {

        LinkedList<Integer> linkedList = null;
        LinkedList<Integer> linkedList2 = new LinkedList<>();

        linkedList2.addAll(linkedList);

        System.out.println(linkedList2);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toArray()" because "c" is null at java.base/java.util.LinkedList.addAll(LinkedList.java:412) at java.base/java.util.LinkedList.addAll(LinkedList.java:391)


Some other methods of LinkedList 

add(K)This method appends the specified element to the end of this list.

add(int, K)This method inserts the specified element at the specified position in this list.

addAll(int, Collection)This method inserts all of the elements in the specified collection into this list, starting at the specified position.

addFirst(K)This method inserts the specified element at the beginning of this list.

addLast(K e)This method appends the specified element to the end of this list.

clear()This method removes all of the elements from this list.

clone()This method returns a shallow copy of this LinkedList.

contains(Object)This method returns true if this list contains the specified

containsAll(Collection)This method returns true if this collection contains all of the elements in the specified collection.

descendingIterator()This method returns an iterator over the elements in this deque in reverse sequential order.

element()This method retrieves but does not remove, the head (first element) of this list.

equals(Object)his method compares the specified object with this list for equality.

forEach(Consumer)This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

get(int)This method returns the element at the specified position in this list.

getFirst()This method returns the first element in this list.

getLast()This method returns the last element in this list.

hashCode()This method returns the hash code value for this list.

indexOf(Object)This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

isEmpty()This method returns true if this collection contains no elements.

iterator()This method returns an iterator over the elements in this list.

lastIndexOf(Object)This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

listIterator()This method returns a list iterator over the elements in this list.

listIterator(int)This method returns a list-iterator of the elements in this list starting at the specified position in the list.

offer(K)This method adds the specified element as the tail (last element) of this list.

offerFirst(K)This method inserts the specified element at the front of this list.

offerLast(K)This method inserts the specified element at the end of this list.

parallelStream()This method returns a possibly parallel Stream with this collection as its source.

peek()This method retrieves but does not remove, the head (first element) of this list.

peekFirst()This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty.

peekLast()This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty.

poll()This method retrieves and removes the head (first element) of this list.

pollFirst()This method retrieves and removes the first element of this list, or returns null if this list is empty.

pollLast()This method retrieves and removes the last element of this list, or returns null if this list is empty.

pop()This method pops an element from the stack represented by this list.

push(K e)This method pushes an element onto the stack represented by this list.

remove()This method retrieves and removes the head (first element) of this list.

remove(int)This method removes the element at the specified position in this list.

remove(Object)This method removes the first occurrence of the specified element from this list if it is present.

removeAll(Collection)This method removes all of this collection's elements that are also contained in the specified collection.

removeFirst()This method removes and returns the first element from this list.

removeFirstOccurrence(Object)This method removes the first occurrence of the specified element in this list.

removeIf(Predicate)This method removes all of the elements of this collection that satisfy the given predicate.

removeLast()This method removes and returns the last element from this list.

removeLastOccurrence(Object)This method removes the last occurrence of the specified element in this list.

replaceAll(UnaryOperator)This method replaces each element of this list with the result of applying the operator to that element.

retainAll(Collection)This method retains only the elements in this collection that are contained in the specified collection.

set(int, K)This method replaces the element at the specified position in this list with the specified element.

size()This method returns the number of elements in this list.

sort(Comparator)This method sorts this list according to the order induced by the specified Comparator.

spliterator()This method creates a late-binding and fail-fast Spliterator over the elements in this list.

stream()This method returns a sequential Stream with this collection as its source.

subList(int, int)This method returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

toArray()This method returns an array containing all of the elements in this list in proper sequence (from the first to the last element).

toArray(IntFunction)This method returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.

toArray(K[])This method returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.

toString()This method returns a string representation of this collection.

No comments:

Post a Comment