Working With LinkedList (Adding,Removing and Retrieving Elements) in Java

In Java, LinkedList implements the list interface as well as Queue interface. Thus LinkedList is not same as ArrayList in Java as ArrayList implements the list interface only.

In this post, we are going to learn how to work with LinkedList in Java. We will learn the following things with the proper example so that it becomes easier to understand for learners.

Create a LinkedList in Java

The below program will create a LinkedList in Java

Actually here we are creating Object of class LinkedList

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<String> list=new LinkedList<String>();
       
        
    }
    
}

Now we have created an object of class LinkedList.



Add elements to the LinkedList in Java

The below java program will add some elements to our list

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<String> list=new LinkedList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
      
    }
    
}

Here we have added some elements.
Now we will learn how to retrieve or print those elements.

Retrieving elements from LinkedList

import java.util.LinkedList;
public class XCodespeedy {
    public static void main(String args[]){
        LinkedList<String> list=new LinkedList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        System.out.println(list);
        
        
    }
    
}

Output

run:
[CodeSpeedy, ArrayList, Java]
BUILD SUCCESSFUL (total time: 0 seconds)

Working with ArrayList in Java ( Adding,Removing elements)

 

Remove Elements from LinkedList in Java

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<String> list=new LinkedList<String>();
        list.add("CodeSpeedy");
        list.add("ArrayList");
        list.add("Java");
        System.out.println(list);
        list.remove(2);
        System.out.println(list);
    }
    
}

Output

run:
[CodeSpeedy, ArrayList, Java]
[CodeSpeedy, ArrayList]
BUILD SUCCESSFUL (total time: 0 seconds)

in remove() method you have to put the index number of your element which you want to be removed from the LinkedList.

Getting a particular element from a LinkedList in Java

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<Integer> list=new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list.get(0));
        
    }
    
}

Output

run:
1
BUILD SUCCESSFUL (total time: 0 seconds)

Updating an element in a LinkedList

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<Integer> list=new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        list.set(2, 6);
    
        System.out.println(list);
    }
    
}

Output

run:
[1, 2, 3]
[1, 2, 6]
BUILD SUCCESSFUL (total time: 0 seconds)

list.set(2, 6);

here first parameter 2 is nothing but the index number of the element which is going to be updated and the 2nd parameter is the new element which I want to be set.

To know the number of elements present in your list you can use the following code:

import java.util.LinkedList;
public class Codespeedy {
    public static void main(String args[]){
        LinkedList<Integer> list=new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list.size());
      
    }
    
}

Output:

run:
3
BUILD SUCCESSFUL (total time: 0 seconds)

Leave a Reply

Your email address will not be published. Required fields are marked *