Polymorphic linked list Java
Java LinkedListIn this tutorial, we will learn about the Java LinkedList in detail with the help of examples. Show
The LinkedList class of the Java collections framework provides the functionality of the linked list data structure (doubly linkedlist). Java Doubly LinkedListEach element in a linked list is known as a node. It consists of 3 fields:
Creating a Java LinkedListHere is how we can create linked lists in Java: LinkedListHere, Type indicates the type of a linked list. For example, // create Integer type linked list LinkedListExample: Create LinkedList in Javaimport java.util.LinkedList; class Main { public static void main(String[] args){ // create linkedlist LinkedListOutput LinkedList: [Dog, Cat, Cow]In the above example, we have created a LinkedList named animals. Here, we have used the add() method to add elements to the LinkedList. We will learn more about the add() method later in this tutorial. Working of a Java LinkedListElements in linked lists are not stored in sequence. Instead, they are scattered and connected through links (Prev and Next). Here we have 3 elements in a linked list.
To learn more, visit the LinkedList Data Structure. Methods of Java LinkedListLinkedList provides various methods that allow us to perform different operations in linked lists. We will look at four commonly used LinkedList Operators in this tutorial:
1. Add elements to a LinkedListWe can use the add() method to add an element (node) at the end of the LinkedList. For example, import java.util.LinkedList; class Main { public static void main(String[] args){ // create linkedlist LinkedListOutput LinkedList: [Dog, Cat, Cow] Updated LinkedList: [Dog, Horse, Cat, Cow]In the above example, we have created a LinkedList named animals. Here, we have used the add() method to add elements to animals. Notice the statement, animals.add(1, "Horse");Here, we have used the index number parameter. It is an optional parameter that specifies the position where the new element is added. To learn more about adding elements to LinkedList, visit Java program to add elements to LinkedList. 2. Access LinkedList elementsThe get() method of the LinkedList class is used to access an element from the LinkedList. For example, import java.util.LinkedList; class Main { public static void main(String[] args) { LinkedListOutput LinkedList: [Python, Java, JavaScript] Element at index 1: JavaIn the above example, we have used the get() method with parameter 1. Here, the method returns the element at index 1. We can also access elements of the LinkedList using the iterator() and the listIterator() method. To learn more, visit the Java program to access elements of LinkedList. 3. Change Elements of a LinkedListThe set() method of LinkedList class is used to change elements of the LinkedList. For example, import java.util.LinkedList; class Main { public static void main(String[] args) { LinkedListOutput LinkedList: [Java, Python, JavaScript, Java] Updated LinkedList: [Java, Python, JavaScript, Kotlin]In the above example, we have created a LinkedList named languages. Notice the line, languages.set(3, "Kotlin");Here, the set() method changes the element at index 3 to Kotlin. 4. Remove element from a LinkedListThe remove() method of the LinkedList class is used to remove an element from the LinkedList. For example, Output LinkedList: [Java, Python, JavaScript, Kotlin] Removed Element: Python New LinkedList: [Java, JavaScript, Kotlin]Here, the remove() method takes the index number as the parameter. And, removes the element specified by the index number. To learn more about removing elements from the linkedlist, visit the Java program to remove elements from LinkedList.. Other Methods
LinkedList as Deque and QueueSince the LinkedList class also implements the Queue and the Deque interface, it can implement methods of these interfaces as well. Here are some of the commonly used methods:
Example: Java LinkedList as Queueimport java.util.LinkedList; import java.util.Queue; class Main { public static void main(String[] args) { QueueOutput LinkedList: [Python, Java, C] Accessed Element: Python Removed Element: Python LinkedList after poll(): [Java, C] LinkedList after offer(): [Java, C, Swift]Example: LinkedList as Dequeimport java.util.LinkedList; import java.util.Deque; class Main { public static void main(String[] args){ DequeOutput LinkedList: [Cow] LinkedList after addFirst(): [Dog, Cow] LinkedList after addLast(): [Dog, Cow, Zebra] LinkedList after removeFirst(): [Cow, Zebra] LinkedList after removeLast(): [Cow]Iterating through LinkedListWe can use the Java for-each loop to iterate through LinkedList. For example, import java.util.LinkedList; class Main { public static void main(String[] args) { // Creating a linked list LinkedListOutput LinkedList: [Cow, Cat, Dog] Accessing linked list elements: Cow, Cat, Dog,LinkedList Vs. ArrayListBoth the Java ArrayList and LinkedList implements the List interface of the Collections framework. However, there exists some difference between them.
Note: We can also create a LinkedList using interfaces in Java. For example, // create linkedlist using List ListHere, if the LinkedList is created using one interface, then we cannot use methods provided by other interfaces. That is, animals1 cannot use methods specific to Queue and Deque interfaces. Table of Contents
|