Java LinkedList
Java LinkedList uses double lineked list references to store its element
Things to be Take Note about LinkedList:
- In an LinkedList Class,there are no shifting. So the Processing time is fast.
- You can access any LinkedList Elements according to its Array Index Number.
- LinkedList Class is non Synchronized
- LinkedList Class maintain its insertion Order
- LinkedList Class allows duplicate Elements.
In this article. I will create an LinkedList Example Code and step through , the basic method used for an LinkedList Class.
Class LinkedListDemo
- First I created a new Class name “LinkedListDemo”
- Create a new LinkedList Object call “animals”
- And then i use animals.add(); to add elements into the ” animals” list
- After adding i check the the size of the LinkedList
- Print out the LinkedList Size
- Get the Value of the First element and print it out
- Loop through all elements inside the LinkedList
- Remove the last Element in the LinkedList
- Loop through again to print out all element in the linkedlist
package linkedListDemo; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> animals = new LinkedList<String>(); // Adding animals.add("lion"); animals.add("Tiger"); animals.add("Baboon"); // Size int size = animals.size(); System.out.println("The size of the list is: " + size); // Get System.out.println("Check the item on index 1 is: " +animals.get(1)); // Iteration System.out.println("Iteration example"); for (int i = 0; i < size; i++) { System.out.println("Item on index " + i + " is: " + animals.get(i)); } System.out.println("\nNext for loop example"); for (String ans : animals) { System.out.println("The item is: " + ans); } // Remove animals.remove(size - 1); System.out.println("\nNext for loop example after removing"); for (String ans : animals) { System.out.println("The item is: " + animals); } List<Integer> aList = new ArrayList<Integer>(); List<Integer> lList = new LinkedList<Integer>(); } }
Check out ArrayList in Java here
and check out also linkedlist documentation here
Leave a Reply