john
john

Reputation: 11709

How to remove first element from the Linked List?

I am trying to remove first element from the LinkedList. Below are the two options. I can see only one works but with the other I see compilation error in my eclipse -

First Approach: This works fine

LinkedList<String> servers = new LinkedList<String>();
....
String firstServerName = servers.removeFirst();

Second Approach In this, I get compilation error in my eclipse as -

The method removeFirst() is undefined for the type List

List<String> servers = new LinkedList<String>();
....
String firstServerName = servers.removeFirst();

What is the difference between the above two? And why first one works but not the second one?

Also what is the most efficient way to remove first element from the linked list in Java? I need to do this operation more frequently on my LinkedList.

Upvotes: 1

Views: 6008

Answers (5)

Nolequen
Nolequen

Reputation: 4337

Since Java 21 there is a SequencedCollection interface, a superinteface for List. Among other it provides removeFirst() method:

List.of(1, 2, 3, 4, 5).removeFirst()

Upvotes: 0

Shivsagar Mitkari
Shivsagar Mitkari

Reputation: 1

in the second approach if u have to use removeFirst() method of LinkedList for List, u have to cast list as follows:

List<String> servers = new LinkedList<String>();
((LinkedList<String>) servers).removeFirst();

else to achieve same can use remove() method

servers.remove(0);

Upvotes: 0

Musaddique
Musaddique

Reputation: 443

LinkedList implement List interface. In List interface removeFirst(0 method is not define. removeFirst() is define and implemented in LinkedList class only. so when you create Object like

List<> list=new LinkedList<>();

then list will access only those method that are declared in List Interface. so you are unable to access removeFirst() by using list.

if you required all methods of LinkedList then used below format:

LinkedList<> lList = new LinkedList<>();

import java.util.*;

public class MyClass
{
    public static void main(String[] args)
    {
        // Create a linked list object:
        LinkedList lList =  new LinkedList();

        // Add some elements:
        lList.add("Isabella");
        lList.add("Angelina");
        lList.add("Pille");
        lList.add("Anabella");

        // Display the elements:
        System.out.println("The old list:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + " = " + lList.get(i));

        // Remove the first element:
           System.out.println(
                  "\nThe following element has been removed: "
                  + lList.removeFirst());

        // Display the new elements:
        System.out.println("\nThe new list:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + " = " + lList.get(i));
    }
}

Upvotes: 0

Frank Andres
Frank Andres

Reputation: 146

removeFirst() is defined for LinkedList which implements List but it is not defined in the interface List. In the first approach it works because servers is defined as a LinkedList. In the second approach it can not work because servers could be any List for example ArrayList for which removeFirst() is not defined.

Upvotes: 1

SomeJavaGuy
SomeJavaGuy

Reputation: 7357

Because removeFirst is a method defined by the exact implementation of the interface List in the class LinkedList and is not part of the methods beeing defined in the interface. If you are saving the Object LinkedList in a more general variable of the type List, then you are allowed to use all the methods that the interface List does know, which removeFirst is not part of.

Upvotes: 0

Related Questions