Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to get/remove first element from the list?

I want to take out and remove first element from the List. I can see, I have two options:

First Approach:

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

Second Approach

ArrayList<String> servers = new ArrayList<String>();
....
String firstServerName = servers.remove(0);

I have lot of elements in my list.

  • 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 if we have lot of elements?

What is the most efficient way to do this.

like image 809
user1950349 Avatar asked Jun 04 '15 00:06

user1950349


People also ask

How do you remove the first element from a list?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do you remove the first value from an ArrayList?

We can use the remove() method of ArrayList container in Java to remove the first element. ArrayList provides two overloaded remove() method: remove(int index) : Accept index of the object to be removed. We can pass the first element's index to the remove() method to delete the first element.

How do I remove the first element from a set in Python?

The pop() is an inbuilt method in Python that is used to pop out or remove the elements one by one from the set. The element that is the smallest in the set is removed first followed by removing elements in increasing order.

What are the two methods for removing items from a list?

The methods are remove(), pop() and clear(). It helps to remove the very first given element matching from the list. The pop() method removes an element from the list based on the index given. The clear() method will remove all the elements present in the list.


2 Answers

List.subList​(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.

Good to use for ArrayList where removing the first element has complexity O(n).

final String firstServerName = servers.get(0);
servers = servers.subList(1, servers.size());
like image 105
Roland Avatar answered Nov 13 '22 14:11

Roland


In practical terms, LinkedList#removeFirst is more efficient since it's operating over a doubly-linked list and the removal of first element basically consist in only unlinking it from the head of the list and update the next element to be the first one:

private E unlinkFirst(Node<E> f) {
    // assert f == first && f != null;
    final E element = f.item;
    final Node<E> next = f.next;
    f.item = null;
    f.next = null; // help GC
    first = next;
    if (next == null)
        last = null;
    else
        next.prev = null;
    size--;
    modCount++;
    return element;
}

ArrayList#remove is operating over an internal array which requires shiftting all subsequents elements one position to the left by copying over the subarray:

public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    elementData[--size] = null; // clear to let GC do its work

    return oldValue;
}

On the other hand, LinkedList#get operation requires traversing half of the entire list for retrieving an element at a specified index - in worst case scenario. ArrayList#get would directly access the element at the specified index since it's operating over an array.

My rule of thumbs for efficiency here would be:

  • Use LinkedList if you do a lot of add/remove in comparison with retrieval operations (e.g.: get);
  • Use ArrayList if you do a lot of retrieval operations in comparison with add/remove.
like image 43
Ben-Hur Langoni Junior Avatar answered Nov 13 '22 12:11

Ben-Hur Langoni Junior