LinkedList is a sequence of elements which will connected as links. The first step toward implementing the LinkedList in Java is to import the Java.util.LinkedList package. After importing this package, you can follow the syntax below to create a LinkedList in Java
LinkedList<Type> linkedList = new LinkedList<>() |
There are different types of LinkedList in Java
Simple Linked List − Item Navigation will be only in forward direction.
Doubly Linked List − Items can Navigate in both directions forward or backward.
Circular Linked List − In Circular LinkedList Last item will maintain the link of the first element as next and first element maintain link to last element as prev.
With LinkedList we will perform below operations
Insertion − This operation will add an element at the beginning of the list.
Deletion − This operation will delete an element at the beginning of the list.
Display − This operation will displaying all elements inside list.
Search − This operation will perform search operation using given key.
Delete − This operation will delete an element based on given key
Sort - Sort the List based on a specific order
Reverse - Reorder the list in reverse order
The Type of the LinkedList can either be an integer or a string type or any type
In this example, we are going to create a LinkedList named students
import java.util.LinkedList; public class LinkedListExample2 { public static void main(String[] args) { // create Student LinkedList LinkedList<String> students = new LinkedList<>(); } } |
We can also create LinkedList with Collection as argument in Constructor
LinkedList(Collection c) |
To add elements to LinkedList we will use add() method of LinkedList
import java.util.LinkedList; public class LinkedListExample2 { public static void main(String[] args) { // create Student LinkedList LinkedList<String> students = new LinkedList<>(); // Add elements students.add("Sumedh"); students.add("Pohekaar"); students.add("Shamir"); System.out.println("The Styudents LinkedList: " + students); } } |
Other methods available with LinkedList
void add(int index, Object element) This method will be used to add an element at given index position. If the given index not available it will throws IndexOutOfBoundsException
boolean add(Object o) This method will add the element at end of the List.
boolean addAll(Collection c) This method will add all elements of the given list to the current list.
boolean addAll(int index, Collection c) This method will insert the given list to the respected position in the current list. If the given index not available it will throws IndexOutOfBoundsException
void addFirst(Object o) This method will insert the element at first position.
void addLast(Object o) This method will append the element at the end position
void clear() This method will remove the all elements of the list.
Object clone() This method will returns the shallow copy of the list.
boolean contains(Object o) This method will be used check an element is available in the list or not. It will returns true if the list contains the elements otherwise returns false.
Object get(int index) This method will used to get an element from the list with given index position. If the given index not available it will throws IndexOutOfBoundsException
Object getFirst() This will returns the first element of the list. If the list is empty it will Throws NoSuchElementException
Object getLast() This will returns the last element of the list. If the list is empty it will Throws NoSuchElementException
int indexOf(Object o) This method will returns the index of given element inside list. If the element not available it will return -1
int lastIndexOf(Object o) This method will returns the index of given element at the last occurrence inside list. If the element not available it will return -1
ListIterator listIterator(int index) Returns the list iterator of the elements in the given list.
Object remove(int index) This method will used to remove the element at the given index.
boolean remove(Object o) This method will remove the given object from the list. If the element not available in the list it will throws NoSuchElementException
Object removeFirst() This method will removes the element at the first. If the list is empty it will Throws NoSuchElementException
Object removeLast() This method will removes the element at the last. If the list is empty it will Throws NoSuchElementException
Object set(int index, Object element) This method will add the element at the given index inside the list. If the respected index not available it will throws IndexOutOfBoundsException
int size() This method will returns the size of the given list.
Object[] toArray() This method will returns array from the given list.
Article Contributed By :
|
|
|
|
257 Views |