Removing Element From LinkedList for Integer Type and String Type

By Saruque Ahamed Mollick

In this post, We are going to know an interesting fact of remove() method used in a LinkedList object.

at the end of this lesson, we will learn how to remove an element without knowing its index.



So, at first, let’s see how remove() method works on a LinkedList object.

You can follow this linkĀ Working With LinkedList (Adding,Removing and Retrieving Elements) in Java

Remove elements from Integer Type LinkedList

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

Output:

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

list.remove(index)— here we put 2 as the index. index of elements start from zero, so index number 2 holds the value 3. Thus element 3 is removed by this method.

Now let’s see what happens with the same method for String Type LinkedList

Remove elements from String Type LinkedList

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

Output:

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

Here “LinkedList” is the element of index number 2.

So “LinkedList” is removed.

Till now everything is fine and easy to understand and now we will see the interesting part

Try to understand it with the following example.

import java.util.LinkedList;
public class Problem2 {
    public static void main(String args[]){
        LinkedList<String> list=new LinkedList<String>();
        list.add("Java");
        list.add("J2EE");
        list.add("LinkedList");
        list.remove("J2EE"); //it will remove the element J2EE
        
              System.out.println(list);

    }
    
}

Here you can simply put the string element in the remove method to remove the element.

Output

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

Now see this

import java.util.LinkedList;
public class problems {
    public static void main(String args[]){
        LinkedList<Integer> list=new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.remove(2); // it will remove the element of index 2 , i.e 3
        
              System.out.println(list);
    }
    
}

Output

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

So you can now understand that in remove method you can pass the index number to remove an element for Integer type LinkedList, But for the String type, you can directly remove an element by putting the string value.

Now you might thought how can you remove an Integer element from a LinkedList if you don’t know the index of that element

How to Remove an Integer Element from a List if you don’t know its index

import java.util.LinkedList;
public class problems {
    public static void main(String args[]){
        LinkedList<Integer> list=new LinkedList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.remove((Integer) 2); // it will remove the element whose value is 2
        
              System.out.println(list);
    }
    
}

Output:

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

in this way you can remove an element even if you don’t know its index number. You can delete a particular integer element directly using the above code.

You can apply this method for ArrayList too

Working with ArrayList in Java ( Adding,Removing elements)

 

Leave a Reply

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