Core JAVA
Java Development KIT (JDK)
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java Runtime Environment (JRE)
Java Virtual Machine (JVM)
Java overview
Java basics
Java Objects and classes
Java Constructors
Java basic datatypes
Java variable types
Java modifiers/Access Modifiers In Java
Java Basic Operators
Java Loops and Controls
Java conditions
Java numbers and characters
Java strings
Java arrays
Java date time
Java methods
Java file and IO operations
Java exceptions
Inner class
Java OOPs Concepts
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java Interface
Cohesion and Coupling
Association, Aggregation and Composition
Java Collections
Java ArrayList
Java LinkedList
Set and HashSet
LinkedHashSet and TreeSet
Queue and PriorityQueue
Deque and PriorityQueue
Java Map Interface
Java HashMap
Internal Working Of Java HashMap
Java Mutithread
Methods of Thread In Java
Join , run & Start Method in Threads
Difference b/w start & run Methods in Threads
Java Concurrency Package & its Features
CountDownLatch, CyclicBarrier, Semaphore and Mutex in Thread
Java LinkedList
Java LinkedList is a class which extends AbstractSequentialList which implements List interface. LinkedList can store non-primitive data types as elements and form a list. LinkedList is ordered list that means elements are arranged in same order they are added.
· LinkedList may contain duplicate elements.
· LinkedList is not synchronized.
· LinkedList manipulation is faster than ArrayList as we can easily remove by element by breaking from link.
LinkedList Declaration
Non Generic Style
LinkedList myList = new LinkedList();
Generic Style
LinkedList myList = new LinkedList ();
LinkedList Constructors
Constructor | Description |
LinkedList( | Creates empty linked list |
LinkedList(Collection<? extends E> c) | ArrayList initialized with some collection. |
LinkedList Methods
Method | Description |
boolean add(E e) | Ads an element |
void add(int index, E element) | Ads an element at an index |
boolean addAll(Collection<? extends E> c) | Adds a collection. |
boolean addAll(Collection<? extends E> c) | Adds a collection. |
boolean addAll(int index, Collection<? extends E> c) | Adds a collection at an index. |
void addFirst(E e) | Adds an element at start of list. |
void addLast(E e) | Adds an element at the end of list. |
void clear() | Clears. |
Object clone() | Creates clone of linked list. |
boolean contains(Object o) | Checks if objects is present in linkedlist. |
Iterator descendingIterator() | Returns iterator. |
E element() | Returns first element. |
E get(int index) | Returns element at given index. |
E getFirst() | Returns first element. |
E getLast() | Returns last element. |
int indexOf(Object o) | Returns index of given object in list. |
int lastIndexOf(Object o) | Returns last index of given object in linked list. |
ListIterator listIterator(int index) | Returns list iterator. |
boolean offer(E e) | Adds element as last element. |
boolean offerFirst(E e) | Adds element at first position of list. |
boolean offerLast(E e) | Adds element at last position of list. |
E peek() | Retruns first element. |
E peekFirst() | Returns first element. |
E peekLast() | Returns last element or null if not present. |
E poll() | Removes first element. |
E pollFirst() | Removes and returns first element. |
E pollLast() | Removes and returns last element. |
E pop() | It pops first element of stack represented by list. |
void push(E e) | It pushes an element on stack represented by list. |
E remove() | Removes element. |
E remove(int index) | Removes element at given index. |
boolean remove(Object o) | Removes given object from list. |
E removeFirst() | Removes first element. |
boolean removeFirstOccurrence(Object o) | Removes first occurrence. |
E removeLast() | Removes last element. |
boolean removeLastOccurrence(Object o) | Removes last occurance. |
E set(int index, E element) | Replaces element at given index. |
Object[] toArray() | Returns array of list |
T[] toArray(T[] a) | Returns array of list |
int size() | Returns size of linkedlist. |
LinkedList Example With Iterating Using Iterator()
import java.util.*;class CollectionsExample3{ public static void main(String args[]){ //create list LinkedList list=new LinkedList(); //add elements in list list.add(“Tom”); list.add(“Bob”); Iterator itr=list.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } } Output
Tom Bob
Iterating LinkedList or collection with forEach, for, ListIterator and forEachRemaining
Example:import java.util.LinkedList; import java.util.ListIterator;public class CollectionsExample3 { public static void main(String[] args) { LinkedList myList=new LinkedList<>(); myList.add(“John”); myList.add(“Ron”); //foreach myList.forEach(data->System.out.println(“forEach: “+data)); //for for (String data : myList) { System.out.println(“for: “+data); } //list itr ListIterator ltr = myList.listIterator(); while(ltr.hasNext()) { System.out.println(“listIterator: “+ltr.next()); } //forEachRemaining of iterator ltr.forEachRemaining(data->{ System.out.println(“forEachRemaining: “+data); }); } } Output:
forEach: John forEach: Ron for: John for: Ron listIterator: John listIterator: Ron
LinkedList/Collections examples of add, addAll, remove, removeAll and retainAll()
import java.util.LinkedList; public class CollectionsExample3 { public static void main(String[] args) { LinkedList myList=new LinkedList<>(); LinkedList myList2=new LinkedList<>(); //add myList.add("John"); myList.add("Ron"); myList.add("Jone"); myList.add("Tony"); myList2.add("Tony"); System.out.println(myList); //addAll myList.addAll(myList2); //remove myList.remove("John"); //removeAll myList.removeAll(myList2); System.out.println(myList); //retainAll myList.add("Tony"); myList.retainAll(myList2); System.out.println(myList); } }Output
[John, Ron, Jone, Tony] [Ron, Jone] [Tony]
Sorting the LinkedList
We can use collections class’ sorting methods to sort it. Syntax: Collections.sort();Next Article
- Set and HashSet