The Collection framework is found in java.util package includes Linked List. This class is an implementation of the LinkedList data structure, a linear data structure where each element is a separate object with a data part and address part and is not kept in a single location. Pointers and addresses are used to connect the elements. Every element is referred to as a node.
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
The following are key details of the Java LinkedList:
1)LinkedList(): An empty linked list can be created with this constructor. The following code can be used to create an empty LinkedList with the name linkobj:
LinkedList linkobj = new LinkedList();
2)LinkedList(Collection C): This constructor is used to produce an ordered list that includes each element returned by the collection’s iterator for a given collection.
LinkedList linkobj = new LinkedList(C);
Method | Description |
---|---|
boolean add(E e) | The specified element is added to the end of the list using this method. |
void add(int index, E element) | It is used to insert the designated element into a list at the designated position index. |
boolean addAll(Collection<? extends E> c) | In the order that the iterator for the supplied collection returns the elements, it is used to append all of the elements in the specified collection to the end of this list. |
boolean addAll(Collection<? extends E> c) | It is used to add each element from the provided collection, in the order that the iterator for that collection returns them, to the end of this list. |
boolean addAll(int index, Collection<? extends E> c) | All the elements in the given collection are appended using this method, beginning at the specified place in the list. |
void addFirst(E e) | It is used to add the specified element to the start of a list. |
void addLast(E e) | The specified element is appended to the end of the list using this method. |
void clear() | It is used to remove all the elements from a list. |
Object clone() | It is used to return a shallow copy of an ArrayList. |
boolean contains(Object o) | Returns true if a list contains a specified element, otherwise false. |
Iterator<E> descendingIterator() | It is used to return an iterator that iterates across a deque’s elements in reverse chronological order. |
E element() | It is used to retrieve the first element of a list. |
E get(int index) | It is used to retrieve the element from a list at the requested location. |
E getFirst() | It is used to retrieve the list’s initial element. |
E getLast() | It is used to return the last element in a list. |
int indexOf(Object o) | It is used to return the index of the first instance of the supplied element in a list, or -1 if the list is empty. |
int lastIndexOf(Object o) | It is used to return the index of the final instance of the supplied element in a list, or -1 if the list is empty. |
ListIterator<E> listIterator(int index) | It is used to return a list-iterator of the elements starting at the provided point in the list and iterating over them in the correct order. |
boolean offer(E e) | It adds the specified element as the last element of a list. |
boolean offerFirst(E e) | It adds the desired element to the list’s front. |
boolean offerLast(E e) | It inserts the specified element at the end of a list. |
E peek() | It retrieves the first element of a list |
E peekFirst() | If a list is empty, it returns null; otherwise, it returns the first entry in the list. |
E peekLast() | It retrieves the last element of a list or returns null if a list is empty. |
E poll() | It retrieves and removes the first element of a list. |
E pollFirst() | It retrieves and removes the first element of a list, or returns null if a list is empty. |
E pollLast() | It retrieves and removes the last element of a list, or returns null if a list is empty. |
E pop() | It pops an element from the stack represented by a list. |
void push(E e) | It pushes an element onto the stack represented by a list. |
E remove() | It is used to retrieve and removes the first element of a list. |
E remove(int index) | It is used to remove the element at the specified position in a list. |
boolean remove(Object o) | It is used to remove the first occurrence of the specified element in a list. |
E removeFirst() | It removes and returns the first element from a list. |
boolean removeFirstOccurrence(Object o) | It is utilised to eliminate the list’s first instance of the supplied element (when traversing the list from head to tail). |
E removeLast() | It removes and returns the last element from a list. |
boolean removeLastOccurrence(Object o) | It removes the last occurrence of the specified element in a list (when traversing the list from head to tail). |
E set(int index, E element) | It replaces the element at the specified position in a list with the specified element. |
Object[] toArray() | It is used to return an array with all of a list’s members in the correct order (from first to the last element). |
<T> T[] toArray(T[] a) | The runtime type of the returned array is the same as the supplied array, and it contains all the elements in the right order (from the first to the final element). |
int size() | It is used to return the number of elements in a list. |
// Java Program to Demonstrate
// Implementation of LinkedList
// class
// Importing required classes
import java.util.*;
// Main class
public class Coderz {
// Main driver method
public static void main(String args[])
{
// Creating object of the
// class linked list
LinkedList<String> linkobj = new LinkedList<String>();
// Adding elements to the linked list
linkobj.add("Animal");
linkobj.add("Beach");
linkobj.addLast("Cat");
linkobj.addFirst("Doll");
linkobj.add(2, "Eel");
System.out.println(linkobj);
linkobj.remove("Beach");
linkobj.remove(3);
linkobj.removeFirst();
linkobj.removeLast();
System.out.println(linkobj);
}
}
[Doll, Animal, Eel, Beach, Cat]
[Animal]
// Java Program to Demonstrate
// Implementation of LinkedList
// class
// Importing required classes
import java.util.*;
// Main class
public class Coderz {
// Main driver method
public static void main(String args[])
{
// Creating object of the
// class linked list
LinkedList<String> linkobj = new LinkedList<String>();
// Adding elements to the linked list
linkobj.add("Animal");
linkobj.add("Beach");
linkobj.addLast("Cat");
linkobj.addFirst("Doll");
linkobj.add(2, "Eel");
System.out.println(linkobj);
Iterator i=linkobj.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
[Doll, Animal, Eel, Beach, Cat]
Cat
Beach
Eel
Animal
Doll
Note: also read about the Java ArrayList in Collection Framework
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…