4

I am trying to return and remove first element from the LinkedList. Below are the two options I can see.

First Approach:

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

Second Approach

List<String> servers = new LinkedList<String>();
....
String firstServerName = servers.remove(0);
  • Is there any preference which one we should use?
  • And what is the difference between the above two? Are they technically same thing in terms of performance? What is the complexity involve here?

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

john
  • 11,311
  • 40
  • 131
  • 251
  • 2
    Did you try looking into the source code? You can check here http://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist to get a comparison which O(x) the various methods have. – GhostCat Apr 08 '15 at 07:41
  • 1
    In that case (removing first element) they do essentially the same work and have the same performance, use whatever you fancy more. `LinkedList` implements few interfaces, `remove(int)` inherited from `java.util.List` interface, `removeFirst` inherited from `java.util.Deque` interface.If you are trying to use LinkedList as queue, consider access it using Queue (or Deque) interface for improved clarity. – weaknespase Nov 13 '18 at 15:35

3 Answers3

5

removeFirst(): Remove the first element in the list. -> O(1)

remove(index): Removes the element at the given position from the list. -> O(n)

So, in your case, because you only want to remove the first element, you can choose to removeFirst().

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
2

You can compare them yourself from the source code (for example here: http://developer.classpath.org/doc/java/util/LinkedList-source.html).

removeFirst() is implemented as:

 260:   /**
 261:    * Remove and return the first element in the list.
 262:    *
 263:    * @return the former first element in the list
 264:    * @throws NoSuchElementException if the list is empty
 265:    */
 266:   public T removeFirst()
 267:   {
 268:     if (size == 0)
 269:       throw new NoSuchElementException();
 270:     modCount++;
 271:     size--;
 272:     T r = first.data;
 273: 
 274:     if (first.next != null)
 275:       first.next.previous = null;
 276:     else
 277:       last = null;
 278: 
 279:     first = first.next;
 280: 
 281:     return r;
 282:   }

remove(int) is implemented as :

575:   /**
 576:    * Removes the element at the given position from the list.
 577:    *
 578:    * @param index the location of the element to remove
 579:    * @return the removed element
 580:    * @throws IndexOutOfBoundsException if index &lt; 0 || index &gt; size()
 581:    */
 582:   public T remove(int index)
 583:   {
 584:     checkBoundsExclusive(index);
 585:     Entry<T> e = getEntry(index);
 586:     removeEntry(e);
 587:     return e.data;
 588:   }

 156:   /**
 157:    * Remove an entry from the list. This will adjust size and deal with
 158:    *  `first' and  `last' appropriatly.
 159:    *
 160:    * @param e the entry to remove
 161:    */
 162:   // Package visible for use in nested classes.
 163:   void removeEntry(Entry<T> e)
 164:   {
 165:     modCount++;
 166:     size--;
 167:     if (size == 0)
 168:       first = last = null;
 169:     else
 170:       {
 171:         if (e == first)
 172:           {
 173:             first = e.next;
 174:             e.next.previous = null;
 175:           }
 176:         else if (e == last)
 177:           {
 178:             last = e.previous;
 179:             e.previous.next = null;
 180:           }
 181:         else
 182:           {
 183:             e.next.previous = e.previous;
 184:             e.previous.next = e.next;
 185:           }
 186:       }
 187:   }
Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
0

If what you want is to remove FIRST ELEMENT only, use LinkedList::remove():

E remove()

This method retrieves and removes the head (first element) of this list.

So the best practice in your case is:

String firstServerName = servers.remove();
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • `remove()` isnt really speaking. Does it remove from the end or from the start? Is it a LIFO or FIFO structure? Thus as best practice i would only use `remove()` if the container has been declared as `Queue` instead of the concrete type. Else i prefer addLast/removeFirst to express the intention. The API of java collections surely could be improved. – Markus Kull Apr 08 '15 at 08:03
  • you must do this questions to OP, he does not clarify any of this, so remove() is his best option because **makes sure you remove the first element** no matter LIFO, FIFO or whatever the List has been declared,... so IMHO it's the **best practice**. – Jordi Castilla Apr 08 '15 at 08:05