- Java LinkedList class uses doubly linked list to store the elements. It extends the AbstractList class and implements List and Deque interfaces.
- LinkedList is a doubly-linked list implementation of the
List
and Deque
interfaces
- Java LinkedList class can contain duplicate elements.
- Java LinkedList class maintains insertion order.
- Java LinkedList class is non synchronized.
- In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
- It doesn’t support random access for retrieving values.
- It can be used as list, stack or queue.
class LinkedList<E>
We can create LinkedList for storing String type elements as follows:
LinkedList<String> ll=new LinkedList<String> () ;
We can create LinkedList for Integer String type elements as follows:
LinkedList<Integer > ll=new LinkedList<Integer > () ;
Constructors of LinkedList class as show below:
- LinkedList( ) : This constructor builds an empty linked list.
- LnkedList(Collection c) :This constructor builds a linked list that is initialized with the elements of the collection c.
Methods of LinkedList class:
LinkedList<String> llistobj = new LinkedList<String>();
1) boolean add(Object item): It appends the specified element to the end of this list.
llistobj.add("A");
2) void add(int index, Object item): Inserts the specified element at the specified position(index-1) in this list.
llistobj.add(2, "B");
3) boolean addAll(Collection c): Appends all of the elements in the specified collection to the end of this list. Consider the below example –
LinkedList<String> llistobj = new LinkedList<String>();
ArrayList<String> arraylistobj= new ArrayList<String>();
arraylistobj.add("A");
arraylistobj.add("B");
llistobj.addAll(arraylistobj);
Add all the elements of ArrayList to the LinkedList.
4) boolean addAll(int index, Collection c):Inserts all of the elements in the specified collection at the specified position(index-1) into this list.
llistobj.add(2, arraylist);
5) void addFirst(Object item): It adds the item (or element) at the first position in the list.
llistobj.addFirst("A");
6) void addLast(Object item): Appends the specified element to the end of this list.
llistobj.addLast("A");
7) void clear(): It removes all the elements of a list.
llistobj.clear();
8) boolean contains(Object item): Returns true if this list contains the specified element otherwise false.
boolean var = llistobj.contains("hello");
9) Object clone(): It returns the copy of the list.
For e.g. My linkedList has five items: A, B, C ,D.and E
Object obj= llistobj.clone();
System.out.println(obj);
Output: The output of above code would be:
[A, B, C, D,E]
10) int indexOf(Object item): Returns the index of the first occurrence of the specified element in this list, or return -1 if this list does not contain the element.
llistobj.indexOf("hello");
11) int lastIndexOf(Object item): It returns the index of last occurrence of the specified element in this list, or return -1 if this list does not contain the element .
int index= llistobj.lastIndexOf("hello);
12) Object get(int index): It returns the item of the specified index from the list.
Object var = llistobj.get(2);
13) Object getFirst(): Returns the first element in this list. Throws NoSuchElementException if this list is empty.
Object var = llistobj.getFirst();
14) Object getLast(): Returns the last element in this list. Throws NoSuchElementException if this list is empty.
Object var= llistobj.getLast();
15) Object poll(): It returns and removes the first item of the list.
Object obj = llistobj.poll();
16) Object pollFirst(): Retrieves and removes the first element of this list, or returns null
if this list is empty.
Object obj = llistobj.pollFirst();
17) Object pollLast(): Retrieves and removes the last element of this list, or returns null
if this list is empty.
Object obj = llistobj.pollLast();
18) Object remove(): It removes the first element of the list.
llistobj.remove();
19) Object remove(int index): It removes the item from the list which is present at the specified index.
llistobj.remove(2);
20) Object remove(Object obj): Removes the first occurrence of the specified element from this list, if it is present.
llistobj.remove("A");
21) Object removeFirst(): It removes the first element from the list.
llistobj.removeFirst();
22) Object removeLast(): It removes the last element of the list.
llistobj.removeLast();
23) Object removeFirstOccurrence(Object item): It removes the first occurrence of the specified item.
llistobj.removeFirstOccurrence("A");
24) Object removeLastOccurrence(Object item): It removes the last occurrence of the given element.
llistobj.removeLastOccurrence("A);
25) Object set(int index, Object item): Replaces the element at the specified index in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is is out of range.
llistobj.set(1, "A");
26) int size(): It returns the number of elements of the list.
llistobj.size();
Example of LinkedList in Java
import
java.util.Iterator;
import
java.util.LinkedList;
public
class
MyLinkedListDemo {
public
static
void
main(String a[]){
LinkedList<String> arrl =
new
LinkedList<String>();
arrl.add(
"A"
);
arrl.add(
"B"
);
arrl.add(
"C"
);
arrl.add(
"D"
);
Iterator<String> itr = arrl.iterator();
while
(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Output:
A
B
C
D
Download this example from Here
Another Example of LinkedList in Java
Example for copying ArrayList into at the end of LinkedList.
import
java.util.ArrayList;
import
java.util.LinkedList;
import
java.util.List;
public
class
MyLinkedListDemo1 {
public
static
void
main(String a[]){
LinkedList<String> listobj=
new
LinkedList<String>();
arrl.add(
"A"
);
arrl.add(
"B"
);
arrl.add(
"C"
);
arrl.add(
"D"
);
System.out.println(
"Actual LinkedList:"
+arrl);
List<String> list =
new
ArrayList<String>();
list.add(
"one"
);
list.add(
"two"
);
listobj
.addAll(list); //copy at end of actual linkedlist
System.out.println(
"After Copy LinkedList: "
+listobj);
}
}
Output:
Actual LinkedList:[A, B, C, D]
After Copy LinkedList : [A, B, C, D, one, two]
Download this example from Here