Java获取集合的最后一个元素

时间:2022-07-16 21:58:54

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?

一种解决方案是先toArray(),然后返回数组的最后一个元素。还有其他更好的吗?

7 个解决方案

#1


4  

It is not very efficient solution, but working one:

这不是一个非常有效的解决方案,但工作一个:

public static <T> T getFirstElement(final Iterable<T> elements) {
        if (elements == null) 
            return null;

        return elements.iterator().next();
    }

    public static <T> T getLastElement(final Iterable<T> elements) {
        final Iterator<T> itr = elements.iterator();
        T lastElement = itr.next();

        while(itr.hasNext()) {
            lastElement=itr.next();
        }

        return lastElement;
    }

#2


51  

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);

集合不一定是有序的元素集,因此可能没有“最后”元素的概念。如果你想要一些有序的东西,你可以使用一个具有last()方法的SortedSet。或者您可以使用List并调用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.

如果您确实需要最后一个元素,则应使用List或SortedSet。但是如果你拥有的只是一个Collection而且你真的非常需要最后一个元素,你可以使用toArray(),或者你可以使用Iterator并迭代到列表的末尾。

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;
}

#3


39  

Iterables.getLast from Google Guava. It has some optimization for Lists and SortedSets too.

来自Google Guava的Iterables.getLast。它对Lists和SortedSets也有一些优化。

#4


8  

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)

编辑:您必须将集合转换为列表之前可能是这样的:new ArrayList(coll)

#5


3  

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.

一个合理的解决方案是使用迭代器,如果你对底层Collection一无所知,但确实知道有一个“last”元素。情况并非总是如此,并非订购所有集合。

Object lastElement = null;

for (Iterator collectionItr = c.iterator(); collectionItr.hasNext(); ) {
  lastElement = collectionItr.next();
}

#6


2  

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

Collection接口中没有last()或first()方法。要获取最后一个方法,您可以在List上执行get(size() - 1)或反转List并执行get(0)。除非您正在处理堆栈或队列,否则我认为不需要在任何Collection API中使用last()方法

#7


0  

Or you can use a for-each loop:

或者你可以使用for-each循环:

Collection<X> items = ...;
X last = null;
for (X x : items) last = x;

#1


4  

It is not very efficient solution, but working one:

这不是一个非常有效的解决方案,但工作一个:

public static <T> T getFirstElement(final Iterable<T> elements) {
        if (elements == null) 
            return null;

        return elements.iterator().next();
    }

    public static <T> T getLastElement(final Iterable<T> elements) {
        final Iterator<T> itr = elements.iterator();
        T lastElement = itr.next();

        while(itr.hasNext()) {
            lastElement=itr.next();
        }

        return lastElement;
    }

#2


51  

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);

集合不一定是有序的元素集,因此可能没有“最后”元素的概念。如果你想要一些有序的东西,你可以使用一个具有last()方法的SortedSet。或者您可以使用List并调用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.

如果您确实需要最后一个元素,则应使用List或SortedSet。但是如果你拥有的只是一个Collection而且你真的非常需要最后一个元素,你可以使用toArray(),或者你可以使用Iterator并迭代到列表的末尾。

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;
}

#3


39  

Iterables.getLast from Google Guava. It has some optimization for Lists and SortedSets too.

来自Google Guava的Iterables.getLast。它对Lists和SortedSets也有一些优化。

#4


8  

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)

编辑:您必须将集合转换为列表之前可能是这样的:new ArrayList(coll)

#5


3  

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.

一个合理的解决方案是使用迭代器,如果你对底层Collection一无所知,但确实知道有一个“last”元素。情况并非总是如此,并非订购所有集合。

Object lastElement = null;

for (Iterator collectionItr = c.iterator(); collectionItr.hasNext(); ) {
  lastElement = collectionItr.next();
}

#6


2  

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

Collection接口中没有last()或first()方法。要获取最后一个方法,您可以在List上执行get(size() - 1)或反转List并执行get(0)。除非您正在处理堆栈或队列,否则我认为不需要在任何Collection API中使用last()方法

#7


0  

Or you can use a for-each loop:

或者你可以使用for-each循环:

Collection<X> items = ...;
X last = null;
for (X x : items) last = x;