I have a collection, I want to get the last element of the collection. What's the most straighforward and fast way to do so?
One solution is to first toArray(), and then return the last element of the array. Is there any other better ones?
This question is related to
java
collections
iterator
Or you can use a for-each loop:
Collection<X> items = ...;
X last = null;
for (X x : items) last = x;
A reasonable solution would be to use an iterator if you don't know anything about the underlying Collection, but do know that there is a "last" element. This isn't always the case, not all Collections are ordered.
Object lastElement = null;
for (Iterator collectionItr = c.iterator(); collectionItr.hasNext(); ) {
lastElement = collectionItr.next();
}
Iterables.getLast
from Google Guava.
It has some optimization for List
s and SortedSet
s too.
Well one solution could be:
list.get(list.size()-1)
Edit: You have to convert the collection to a list before maybe like this: new ArrayList(coll)
To avoid some of the problems mentioned above (not robust for nulls etc etc), to get first and last element in a list an approach could be
import java.util.List;
public static final <A> A getLastElement(List<A> list) {
return list != null ? getElement(list, list.size() - 1) : null;
}
public static final <A> A getFirstElement(List<A> list) {
return list != null ? getElement(list, 0) : null;
}
private static final <A> A getElement(List<A> list, int pointer) {
A res = null;
if (list.size() > 0) {
res = list.get(pointer);
}
return res;
}
The convention adopted is that the first/last element of an empty list is null...
If you have Iterable convert to stream and find last element
Iterator<String> sourceIterator = Arrays.asList("one", "two", "three").iterator();
Iterable<String> iterable = () -> sourceIterator;
String last = StreamSupport.stream(iterable.spliterator(), false).reduce((first, second) -> second).orElse(null);
This should work without converting to List/Array:
collectionName.stream().reduce((prev, next) -> next).orElse(null)
There isn't a last()
or first()
method in a Collection interface. For getting the last method, you can either do get(size() - 1)
on a List or reverse the List and do get(0)
. I don't see a need to have last()
method in any Collection API unless you are dealing with Stacks
or Queues
A Collection
is not a necessarily ordered set of elements so there may not be a concept of the "last" element. If you want something that's ordered, you can use a SortedSet
which has a last()
method. Or you can use a List
and call mylist.get(mylist.size()-1);
If you really need the last element you should use a List
or a SortedSet
. But if all you have is a Collection
and you really, really, really need the last element, you could use toArray()
or you could use an Iterator
and iterate to the end of the list.
For example:
public Object getLastElement(final Collection c) {
final Iterator itr = c.iterator();
Object lastElement = itr.next();
while(itr.hasNext()) {
lastElement = itr.next();
}
return lastElement;
}
Source: Stackoverflow.com