Previously we have discussed about
Here we learn what is LinkedList In Java.But before that we must know, What is Collection Interface in Java ?
Class LinkedList<E>
Type Parameters:
E - the type of elements held in this collection
All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
Declaration of LinkedList:
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
LinkedList is Doubly-linked list implementation of the List and Deque interfaces. Implements all list operations, and permits all elements including null.
LinkedList is an example of fail-fast iterator and LinkedList implementation is not synchronized. If multiple threads access an LinkedList instance concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally.To prevent accidental unsynchronized access to the list:
List list = Collections.synchronizedList(new LinkedList(...));
This class is a member of the Java Collections Framework.
Constructor Summary of LinkedList:
Constructor
|
Description
|
LinkedList()
|
Constructs an empty list.
|
LinkedList(Collection<? extends E> c)
|
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's iterator.
|
Method Summary of LinkedList:
Modifier and Method Name
|
Description
|
boolean add(E e)
|
Appends the specified element to the end of this
list.
|
void add(int index, E element)
|
Inserts the specified element at the specified
position in this list.
|
boolean addAll(Collection<? extends E> 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.
|
boolean addAll(int index, Collection<? extends E> c)
|
Inserts all of the elements in the specified
collection into this list, starting at the specified position.
|
void addFirst(E e)
|
Inserts the specified element at the beginning of
this list.
|
void addLast(E e)
|
Appends the specified 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 ArrayList
instance.
|
Iterator<E> descendingIterator()
|
Returns an iterator over the elements in this
deque in reverse sequential order.
|
boolean contains(Object o)
|
Returns true if this list contains the specified
element.
|
E element()
|
Retrieves, but does not remove, the head (first
element) of this list.
|
E get(int index)
|
Returns the element at the specified position in
this list.
|
E getFirst()
|
Returns the first element in this list.
|
E getLast()
|
Returns the last element in this list.
|
int indexOf(Object o)
|
Returns the index of the first occurrence of the
specified element in this list, or -1 if this list does not contain the
element.
|
boolean offer(E e)
|
Adds the specified element as the tail (last
element) of this list.
|
boolean offerFirst(E e)
|
Inserts the specified element at the front of
this list.
|
boolean offerLast(E e)
|
Inserts the specified element at the end of this
list.
|
E peek()
|
Retrieves, but does not remove, the head (first
element) of this list.
|
E peekFirst()
|
Retrieves, but does not remove, the first element
of this list, or returns null if this list is empty.
|
E peekLast()
|
Retrieves, but does not remove, the last element
of this list, or returns null if this list is empty.
|
E poll()
|
Retrieves and removes the head (first element) of
this list.
|
E pollFirst()
|
Retrieves and removes the first element of this
list, or returns null if this list is empty.
|
E pollLast()
|
Retrieves and removes the last element of this
list, or returns null if this list is empty.
|
E pop()
|
Pops an element from the stack represented by
this list.
|
void push(E e)
|
Pushes an element onto the stack represented by
this list.
|
int lastIndexOf(Object o)
|
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<E> listIterator(int index)
|
Returns a list iterator over the elements in this
list (in proper sequence), starting at the specified position in the list.
|
E remove()
|
Retrieves and removes the head (first element) of
this list.
|
E remove(int index)
|
Removes the element at the specified position in this
list.
|
boolean remove(Object o)
|
Removes the first occurrence of the specified
element from this list, if it is present.
|
E removeFirst()
|
Removes and returns the first element from this
list.
|
boolean removeFirstOccurrence(Object o)
|
Removes the first occurrence of the specified
element in this list (when traversing the list from head to tail).
|
E removeLast()
|
Removes and returns the last element from this
list.
|
boolean removeLastOccurrence(Object o)
|
Removes the last occurrence of the specified
element in this list (when traversing the list from head to tail).
|
E set(int index, E element)
|
Replaces the element at the specified position in
this list with the specified element.
|
int size()
|
Returns the number of elements in this list.
|
Object[] toArray()
|
Returns an array containing all of the elements
in this list in proper sequence (from first to last element).
|
<T> T[] toArray(T[] a)
|
Returns an array containing all of the elements
in this list in proper sequence (from first to last element); the runtime
type of the returned array is that of the specified array.
|
Example of LinkedList:
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> ll = new LinkedList<String>();
System.out.println("Initial size of LinkedList : "+ll.size());
ll.add("India");
ll.add("Japan");
ll.add("Russia");
ll.add("USA");
//Use of addFirst(e) method
ll.addFirst("Pushkar");
//Use of addLast(e) method
ll.addLast("Uttam Nagar");
//Removing Elements From LinkedList
ll.remove(1);
System.out.println("Displaying LinkedList : "+ll);
System.out.println("\ngetFirst(),This method give u first element :\t"+ll.getFirst());
System.out.println("getLast(),This method give u last element :\t"+ll.getLast());
System.out.println("element(),This method gives u the First element :\t"+ll.element());
System.out.println("contains(e),This method return true or false :\t"+ll.contains("India"));
System.out.println("indexOf(),This method return index of first Occurence :\t"+ll.indexOf(2));
System.out.println("lastIndexOf(),This method return index of last Occurence :\t"+ll.lastIndexOf(2));
System.out.println("peek(),This method give u the head element :\t"+ll.peek());
System.out.println("peekFirst(),This method give u the first element :\t"+ll.peekFirst());
System.out.println("poll(),This method retrieves & remove the head element :\t"+ll.poll());
System.out.println("pollFirst(),This method retrieves & remove the first element :\t"+ll.pollFirst());
System.out.println("pollLast(),This method retrieves & remove the last element :\t"+ll.pollLast());
System.out.println("pop(),This method removes the first element from stack :\t"+ll.poll());
System.out.println(""+ll.removeFirst());
//This method pushes the elements into LinkedList
ll.push("Banana");
//This method add elements at the tail of LinkedList or as a Last element
ll.offer("New Delhi");
//This method add elements at First position in LinkedList
ll.offerFirst("Dubai");
//This method add elements at last position in LinkedList
ll.offerLast("Apple");
System.out.println("\nLinkedList after elements Remove : "+ll);
System.out.println("\nTraversing LinkedList In Descending Order using descendingIterator() method: ");
Iterator<String> itr = ll.descendingIterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Program Output:
Initial size of LinkedList : 0
Displaying LinkedList : [Pushkar, Japan, Russia, USA, Uttam Nagar]
getFirst(),This method give u first element : Pushkar
getLast(),This method give u last element : Uttam Nagar
element(),This method gives u the First element : Pushkar
contains(e),This method return true or false : false
indexOf(),This method return index of first Occurence : -1
lastIndexOf(),This method return index of last Occurence : -1
peek(),This method give u the head element : Pushkar
peekFirst(),This method give u the first element : Pushkar
poll(),This method retrieves & remove the head element : Pushkar
pollFirst(),This method retrieves & remove the first element : Japan
pollLast(),This method retrieves & remove the last element : Uttam Nagar
pop(),This method removes the first element from stack : Russia
USA
LinkedList after elements Remove : [Dubai, Banana, New Delhi, Apple]
Traversing LinkedList In Descending Order using descendingIterator() method:
Apple
New Delhi
Banana
Dubai
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment