Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 03 Mar 2017
LinkedList class extends AbstractSequentialList and implements the List, Java Queue Interface, and Java Deque Interface interfaces.

Declaration of LinkedList:
class LinkedList< E >
Here, E specifies the type of objects that the list will hold.

LinkedList has two constructors they are shown below.

Constructors in LinkedList:
LinkedList()
Builds an empty linked list.

LinkedList(Collection<? extends E> <code class="code">c</code>)
Builds a linked list with the elements of the collection c.

The following are the methods of LinkedList.
Method Description
void add(int index, Object element) Inserts the specified element at the specified position index in list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index > size()).
boolean add(Object o) Appends the specified element to the end of this list.
boolean addAll(Collection c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. Throws NullPointerException if the specified collection is null.
boolean addAll(int index, Collection c) Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null.
void addFirst(Object o) Inserts the given element at the beginning of this list.
void addLast(Object o) Appends the given element to the end of this list.
void clear() Removes all of the elements from this list.
Object clone() Returns a shallow copy of this LinkedList.
boolean contains(Object o) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
Object get(int index) Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
Object getFirst() Returns the first element in this list. Throws NoSuchElementException if this list is empty.
Object getLast() Returns the last element in this list. Throws NoSuchElementException if this list is empty.
int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
ListIterator listIterator(int index) Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
Object remove(int index) Removes the element at the specified position in this list. Throws NoSuchElementException if this list is empty.
boolean remove(Object o) Removes the first occurrence of the specified element in this list. Throws NoSuchElementException if this list is empty. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
Object removeFirst() Removes and returns the first element from this list. Throws NoSuchElementException if this list is empty.
Object removeLast() Removes and returns the last element from this list. Throws NoSuchElementException if this list is empty.
Object set(int index, Object element) Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
int size() Returns the number of elements in this list.
Object[] toArray() Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null.
Object[] toArray(Object[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.
retainAll(Collection<?> c) Removes all elements from the invoking collection except those in c. Returns true if the collection changed(i.e., elements were removed). Otherwise, returns false.
LinkedListDemo
import java.util.*;

class LinkedListDemo
{
    public static void main(String arg[])
    {
        LinkedList characters = new LinkedList(); // LINE A
        characters.add('A');
        characters.add('C');
        characters.add('D');
        System.out.println("Original list: " + characters);
        characters.add(1, 'B'); // LINE B
        System.out.println("After adding B: " + characters);
        List numbers = new ArrayList(); // LINE C
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        characters.addAll(numbers); // LINE D
        System.out.println("After adding numbers: " + characters);
        characters.addFirst("First"); // LINE E
        characters.addLast("Last"); // LINE F
        System.out.println("After adding First and Last: " + characters); // LINE G
        characters.removeFirst(); // LINE H
        characters.removeLast(); // LINE I
        System.out.println("After removing First and Last:" + characters);
        //converting characters to char[] array
        Object[] newCharacters = characters.toArray(); // LINE J
        System.out.println("Conveting LinkedList to Array of objects: ");
        for (Object newCharacter : newCharacters)
        {
            System.out.print(newCharacter + ", ");
        }
    
    }
}
OUTPUT

Original list: [A, C, D]
After adding B: [A, B, C, D]
After adding numbers: [A, B, C, D, 1, 2, 3]
After adding First and Last: [First, A, B, C, D, 1, 2, 3, Last]
After removing First and Last:[A, B, C, D, 1, 2, 3]
Conveting LinkedList to Array of objects:
A, B, C, D, 1, 2, 3,

DESCRIPTION

At LINE A we have created a LinkedList and added elements to it. At LINE B we have we have added character B to the characters. At LINE C we have created an ArrayList and added numbers to it. At LINE D we have added all the numbers to the end of the characters using addAll method and displayed. At LINE E and LINE F we have added First and Last using addFirst and addLast and displayed it at LINE G. At LINE H and LINE I we have removed First and Last from the list using removeFirst and removeLast methods. At LINE J we have converted characters to array of Objects and printed them using a for-Each loop.

THINGS TO TRY
  • To add numbers to the first of characters replace LINE D with the code below.
    addAll(0, numbers)
  • Replace LINE H and LINE I with the below lines of code.
    characters.pollFirst();
    characters.pollLast();
    since both have the same functionality we don't see any change in the output.
  • Replace LINE J with the code below
    char[] newCharacters = characters.toArray();
    The above line throws a compilation error since objects cannot be converted to primitive data type char.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.