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.
What is the most efficient way to do this.
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.
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.
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.
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.
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());
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:
LinkedList
if you do a lot of add
/remove
in comparison with
retrieval operations (e.g.: get
); ArrayList
if you do a lot
of retrieval operations in comparison with add
/remove
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With