Saturday, April 20, 2019

Linkedlist Java

Key facts about Linkedlist
·  LinkedList can have duplicate and null values.·  The LinkedList class implements Queue and Deque interfaces. Therefore, It can also be used as a QueueDeque or Stack.
·  Java LinkedList is not thread-safe. You must explicitly synchronize concurrent modifications to the LinkedList in a multi-threaded environment.·  Java LinkedList maintains the insertion order of the elements.

Linkedlist Example:import java.util.*;
public class LinkedListExample {
     public static void main(String args[]) {
 
       /* Linked List Declaration */
       LinkedList<String> testlist = new LinkedList<String>();
 
       /*add(String Element) is used for adding 
        * the elements to the linked list*/
       testlist.add("Item1");
       testlist.add("Item5");
       testlist.add("Item3");
       testlist.add("Item6");
       testlist.add("Item2");
 
       /*Display Linked List Content*/
       System.out.println("Linked List Content: " + testlist);
 
       /*Add First and Last Element*/
       testlist.addFirst("First Item");
       testlist.addLast("Last Item");
       System.out.println("LinkedList Content after addition: " + testlist);
 
       /*This is how to get and set Values*/
       Object firstvar = testlist.get(0);
       System.out.println("First element: " +firstvar);
       testlist.set(0, "Changed first item");
       Object firstvar2 = testlist.get(0);
       System.out.println("First element after update by set method: " +firstvar2);
 
       /*Remove first and last element*/
       testlist.removeFirst();
       testlist.removeLast();
       System.out.println("LinkedList after deletion of first and last element: " + testlist);
 
       /* Add to a Position and remove from a position*/
       testlist.add(0, "Newly added item");
       testlist.remove(2);
       System.out.println("Final Content: " + testlist); 
       
       /* Access specific element in the Linkedlist*/
       testlist.indexOf(“Newly added item”);


       /*Iterator for Linkedlist*/
       Iterator<String> testlistIterator = testlist.iterator();
        while (testlistIterator.hasNext()) {
            String speciesName = testlistIterator.next();
            System.out.println(speciesName);
        }

Applications of linked list in real world-
  1. Image viewer – Previous and next images are linked, hence can be accessed by next and previous button.
  2. Previous and next page in web browser – We can access previous and next url searched in web browser by pressing back and next button since, they are linked as linked list.
  3. Music Player – Songs in music player are linked to previous and next song. you can play songs either from starting or ending of the list.

4 comments:

luckys said...

english to marathi typing   

Vishal DurgaIT said...
This comment has been removed by the author.
Durga IT Solutions said...
This comment has been removed by the author.
Quintin said...

Thoughtful blog, thanks for posting