794

How can I get the last value of an ArrayList?

11
  • 216
    I upvoted this question, because I was wondering why there's no such a method like: getLastItem() and came to see if there was an answer. list.size() - 1 is not pretty. Nov 23, 2012 at 17:31
  • 13
    You could always use a LinkedList which has the method getLast()
    – ssedano
    Apr 25, 2014 at 22:56
  • 6
    Linked listing it adds a whole heap of overhead. Use Guava as shown below : lastElement = Iterables.getLast(iterableList); OR simply index a get() call with size() - 1. Its not that ugly compared to using a linked list when its not required. Usual caveats apply regarding exception conditions - see the ArrayList javadoc.
    – RichieHH
    May 23, 2014 at 11:43
  • 18
    Using list.size() -1 is not pretty, but using a 3rd party API just for this is worse
    – Javo
    Jul 19, 2017 at 14:52
  • 3
    @soger No, but "it could be worse" is not a satisfactory reason to not have a readable method like getLastElement() be part of the java.util.List API. Apr 9, 2021 at 5:35

24 Answers 24

866

The following is part of the List interface (which ArrayList implements):

E e = list.get(list.size() - 1);

E is the element type. If the list is empty, get throws an IndexOutOfBoundsException. You can find the whole API documentation here.

12
  • 7
    Will this cause an iteration of the list? That doesnt seem very efficient to me. I come from C++, where there are actual front() and back() methods on the list object, that are internally implemented with head and tail references. Is there a similar mechanism in Java?
    – Brady
    Oct 13, 2014 at 9:18
  • 37
    Won't work. what if the list is empty, list.size() will return 0. and you'll end up with list.get(-1);
    – frankelot
    Jan 21, 2015 at 19:36
  • 28
    @feresr huh. He wants to get the last value in the list. Of course that implies that size() is > 0. That would be true for any sort of implementation. Reading through to the end would have saved the time you needed to write your comment and my time for answering :) My answer says at the end "If the list is empty, get throws an IndexOutOfBoundsException" Jan 22, 2015 at 23:20
  • 22
    @Brady it won't cause an O(n) iteration for an ArrayList, because as you can guess, it's backed by an array. So a simple get(<index>) just results in a constant-time retrieve from an array. (JDK source confirms this) For other list implementations, this isn't guaranteed, so for example, LinkedList has a getLast() method which is constant-time.
    – Peter
    Mar 19, 2015 at 19:58
  • 19
    I can't understand why they decided to implement a simple lastElement() method for their Vector but not for ArrayList. What's up with that inconsistency? Sep 7, 2016 at 9:54
256

There isn't an elegant way in vanilla Java prior to Java 21.

Google Guava

The Google Guava library is great - check out their Iterables.getLast() method. This method will throw a NoSuchElementException if the list is empty, as opposed to an IndexOutOfBoundsException, as with the typical size()-1 approach - I find a NoSuchElementException much nicer, or the ability to specify a default:

lastElement = Iterables.getLast(iterableList);

You can also provide a default value if the list is empty, instead of an exception:

lastElement = Iterables.getLast(iterableList, null);

or, if you're using Options:

lastElementRaw = Iterables.getLast(iterableList, null);
lastElement = (lastElementRaw == null) ? Option.none() : Option.some(lastElementRaw);
5
  • 3
    Do you know if this this method do a linear walk through the list to find the last element?
    – BillMan
    Feb 20, 2013 at 15:51
  • 6
    @BillMan In the case of HashSet yes, in the case of ArrayList no.
    – Simon
    Aug 19, 2013 at 12:50
  • 6
    You should add that Iterables.getLast check whether RandomAccess is implemented and therefore if it is accesses the item in O(1). Jun 22, 2017 at 23:23
  • 2
    Instead of Option, you can use the native Java Optional. It will also be a bit cleaner: lastElement = Optional.ofNullable(lastElementRaw);. May 7, 2019 at 8:15
  • Which Option library did you have in mind with this solution?
    – M. Justin
    Mar 26 at 17:19
201

this should do it:

if (arrayList != null && !arrayList.isEmpty()) {
  T item = arrayList.get(arrayList.size()-1);
}
5
  • 47
    is there no sleek way to do it ? :/ Sep 5, 2012 at 7:55
  • 6
    You should probably at least demonstrate assigning it... ArrayList.get is side-affect free. Dec 28, 2012 at 16:17
  • 2
    Is it too petty to indicate the above doesn't assign/return anything ? Mar 23, 2017 at 10:41
  • If an ArrayList only have one record then there is exception occurred. What will be the solution? Apr 12, 2018 at 12:11
  • 3
    @hasnain_ahmad, when ArraList has 1 element it works properly, you should worry about non-initialized ArrayList and ArrayList with zero record. And this answer handles both cases
    – Farid
    Jul 22, 2018 at 13:48
33

I use micro-util class for getting last (and first) element of list:

public final class Lists {

    private Lists() {
    }

    public static <T> T getFirst(List<T> list) {
        return list != null && !list.isEmpty() ? list.get(0) : null;
    }

    public static <T> T getLast(List<T> list) {
        return list != null && !list.isEmpty() ? list.get(list.size() - 1) : null;
    }
}

Slightly more flexible:

import java.util.List;

/**
 * Convenience class that provides a clearer API for obtaining list elements.
 */
public final class Lists {

  private Lists() {
  }

  /**
   * Returns the first item in the given list, or null if not found.
   *
   * @param <T> The generic list type.
   * @param list The list that may have a first item.
   *
   * @return null if the list is null or there is no first item.
   */
  public static <T> T getFirst( final List<T> list ) {
    return getFirst( list, null );
  }

  /**
   * Returns the last item in the given list, or null if not found.
   *
   * @param <T> The generic list type.
   * @param list The list that may have a last item.
   *
   * @return null if the list is null or there is no last item.
   */
  public static <T> T getLast( final List<T> list ) {
    return getLast( list, null );
  }

  /**
   * Returns the first item in the given list, or t if not found.
   *
   * @param <T> The generic list type.
   * @param list The list that may have a first item.
   * @param t The default return value.
   *
   * @return null if the list is null or there is no first item.
   */
  public static <T> T getFirst( final List<T> list, final T t ) {
    return isEmpty( list ) ? t : list.get( 0 );
  }

  /**
   * Returns the last item in the given list, or t if not found.
   *
   * @param <T> The generic list type.
   * @param list The list that may have a last item.
   * @param t The default return value.
   *
   * @return null if the list is null or there is no last item.
   */
  public static <T> T getLast( final List<T> list, final T t ) {
    return isEmpty( list ) ? t : list.get( list.size() - 1 );
  }

  /**
   * Returns true if the given list is null or empty.
   *
   * @param <T> The generic list type.
   * @param list The list that has a last item.
   *
   * @return true The list is empty.
   */
  public static <T> boolean isEmpty( final List<T> list ) {
    return list == null || list.isEmpty();
  }
}
5
  • 12
    Just use guava. Don't re-invent
    – Ali
    Nov 4, 2016 at 18:49
  • 25
    @ClickUpvote Using Guava for just one tiny method is in many cases an overkill. My answer is for people looking for vanilla Java solution. If you are already using Guava in your project, see other answer for Guava-based solution.
    – user11153
    Nov 4, 2016 at 18:54
  • 6
    If you don't use guava, you end up writing lots of utility classes like this one.
    – Ali
    Nov 5, 2016 at 15:02
  • 11
    Sometimes getting permission to add a third-party library can be a lot more involved than adding a single native Java class. For example, government contracts where they limit and screen third-party libraries. Mar 31, 2017 at 18:09
  • 2
    isEmpty doesn't check whether the list is empty and should thus be isNullOrEmpty and that's not part of the question - either you try to enhance the set of answers or you provide you utility classes (which are a re-invention). Jun 22, 2017 at 23:25
16

List#getLast

Starting with Java 21, the list's getLast method can be used. This returns the last item of the list, throwing a NoSuchElementException if the list is empty.

list.getLast();
1
15

The size() method returns the number of elements in the ArrayList. The index values of the elements are 0 through (size()-1), so you would use myArrayList.get(myArrayList.size()-1) to retrieve the last element.

8

There is no elegant way of getting the last element of a list in Java (compared to e.g. items[-1] in Python).

You have to use list.get(list.size()-1).

When working with lists obtained by complicated method calls, the workaround lies in temporary variable:

List<E> list = someObject.someMethod(someArgument, anotherObject.anotherMethod());
return list.get(list.size()-1);

This is the only option to avoid ugly and often expensive or even not working version:

return someObject.someMethod(someArgument, anotherObject.anotherMethod()).get(
    someObject.someMethod(someArgument, anotherObject.anotherMethod()).size() - 1
);

It would be nice if fix for this design flaw was introduced to Java API.

4
  • I do not see a "design flaw" here, what you bring up is a rare use case that's not worth to be added to the List interface. Why would you want to call a method returning a List, if you're only interested in the last element? I do not remember I have seen that at all before. Oct 20, 2019 at 9:44
  • 3
    @DorianGray Reading last element from a list is a pretty common operation and list.get(list.size()-1) is the minimal example showing the issue. I agree that the "advanced" examples may be controversial and possibly an edge case, I just wanted to show how the issue can further propagate. Let's assume that the class of someObject is foreign, coming from external library.
    – Tregoreg
    Oct 20, 2019 at 14:07
  • I do not see where this is pretty common, and if it is, you better use ArrayDeque instead. Oct 20, 2019 at 16:39
  • 1
    @DorianGray This question has a lot of upvotes and views, so there is a lot of people interested in getting the last value of an ArrayList.
    – Tregoreg
    Oct 20, 2019 at 16:51
7

Using lambdas:

Function<ArrayList<T>, T> getLast = a -> a.get(a.size() - 1);
6

If you use a LinkedList instead , you can access the first element and the last one with just getFirst() and getLast() (if you want a cleaner way than size() -1 and get(0))

Implementation

Declare a LinkedList

LinkedList<Object> mLinkedList = new LinkedList<>();

Then this are the methods you can use to get what you want, in this case we are talking about FIRST and LAST element of a list

/**
     * Returns the first element in this list.
     *
     * @return the first element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return f.item;
    }

    /**
     * Returns the last element in this list.
     *
     * @return the last element in this list
     * @throws NoSuchElementException if this list is empty
     */
    public E getLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return l.item;
    }

    /**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

    /**
     * Removes and returns the last element from this list.
     *
     * @return the last element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeLast() {
        final Node<E> l = last;
        if (l == null)
            throw new NoSuchElementException();
        return unlinkLast(l);
    }

    /**
     * Inserts the specified element at the beginning of this list.
     *
     * @param e the element to add
     */
    public void addFirst(E e) {
        linkFirst(e);
    }

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #add}.
     *
     * @param e the element to add
     */
    public void addLast(E e) {
        linkLast(e);
    }

So , then you can use

mLinkedList.getLast(); 

to get the last element of the list.

1
  • Best response here!
    – marcolopes
    Sep 30, 2020 at 23:58
6

As stated in the solution, if the List is empty then an IndexOutOfBoundsException is thrown. A better solution is to use the Optional type:

public class ListUtils {
    public static <T> Optional<T> last(List<T> list) {
        return list.isEmpty() ? Optional.empty() : Optional.of(list.get(list.size() - 1));
    }
}

As you'd expect, the last element of the list is returned as an Optional:

var list = List.of(10, 20, 30);
assert ListUtils.last(list).orElse(-1) == 30;

It also deals gracefully with empty lists as well:

var emptyList = List.<Integer>of();
assert ListUtils.last(emptyList).orElse(-1) == -1;
6

In case you have a Spring project, you can also use the CollectionUtils.lastElement from Spring (javadoc), so you don't need to add an extra dependency like Google Guava.

It is null-safe so if you pass null, you will simply receive null in return. Be careful when handling the response though.

Here are somes unit test to demonstrate them:

@Test
void lastElementOfList() {
    var names = List.of("John", "Jane");

    var lastName = CollectionUtils.lastElement(names);

    then(lastName)
        .as("Expected Jane to be the last name in the list")
        .isEqualTo("Jane");
}

@Test
void lastElementOfSet() {
    var names = new TreeSet<>(Set.of("Jane", "John", "James"));

    var lastName = CollectionUtils.lastElement(names);

    then(lastName)
        .as("Expected John to be the last name in the list")
        .isEqualTo("John");
}

Note: org.assertj.core.api.BDDAssertions#then(java.lang.String) is used for assertions.

5

If you can, swap out the ArrayList for an ArrayDeque, which has convenient methods like removeLast.

2
  • 1
    This means at least linear cost in comparison to constant cost for direct access, but worth mentioning. Jun 22, 2017 at 23:28
  • @KarlRichter Yes. This corresponds with the absence of methods like get(int) in ArrayDeque's interface. This is what I meant to suggest by "If you can"; if the list is not being accessed by index, then perhaps it doesn't need to be a List. Jul 13, 2017 at 5:09
4

A one liner that takes into account empty lists would be:

T lastItem = list.size() == 0 ? null : list.get(list.size() - 1);

Or if you don't like null values (and performance isn't an issue):

Optional<T> lastItem = list.stream().reduce((first, second) -> second);
1

Since the indexing in ArrayList starts from 0 and ends one place before the actual size hence the correct statement to return the last arraylist element would be:

int last = mylist.get(mylist.size()-1);

For example:

if size of array list is 5, then size-1 = 4 would return the last array element.

1

guava provides another way to obtain the last element from a List:

last = Lists.reverse(list).get(0)

if the provided list is empty it throws an IndexOutOfBoundsException

4
  • 1
    java.util.Collections#reverse does it, too.
    – RoBeaToZ
    Apr 22, 2020 at 9:00
  • 1
    @RoBeaToZ, it does but it changes the original list by iterating through it and returns void, so it doesn't deem to fit for this purpose.
    – pero_hero
    Apr 22, 2020 at 13:30
  • That's pretty expensive for just getting one element. Imagine your list has 1.000.000 entries or so.
    – philburns
    Nov 24, 2020 at 13:33
  • @philburns this call is in O(1) regardless of the list size
    – pero_hero
    Apr 14, 2021 at 11:05
-1

This worked for me.

private ArrayList<String> meals;
public String take(){
  return meals.remove(meals.size()-1);
}
-2

The last item in the list is list.size() - 1. The collection is backed by an array and arrays start at index 0.

So element 1 in the list is at index 0 in the array

Element 2 in the list is at index 1 in the array

Element 3 in the list is at index 2 in the array

and so on..

1
  • 4
    no additional value to @JohannesSchaub's earlier answer Jun 22, 2017 at 23:27
-3

If you modify your list, then use listIterator() and iterate from last index (that is size()-1 respectively). If you fail again, check your list structure.

-4

How about this.. Somewhere in your class...

List<E> list = new ArrayList<E>();
private int i = -1;
    public void addObjToList(E elt){
        i++;
        list.add(elt);
    }


    public E getObjFromList(){
        if(i == -1){ 
            //If list is empty handle the way you would like to... I am returning a null object
            return null; // or throw an exception
        }

        E object = list.get(i);
        list.remove(i); //Optional - makes list work like a stack
        i--;            //Optional - makes list work like a stack
        return object;
    }
-4

All you need to do is use size() to get the last value of the Arraylist. For ex. if you ArrayList of integers, then to get last value you will have to

int lastValue = arrList.get(arrList.size()-1);

Remember, elements in an Arraylist can be accessed using index values. Therefore, ArrayLists are generally used to search items.

1
  • 6
    no additional value to @JohannesSchaub's earlier answer Jun 22, 2017 at 23:27
-5

arrays store their size in a local variable called 'length'. Given an array named "a" you could use the following to reference the last index without knowing the index value

a[a.length-1]

to assign a value of 5 to this last index you would use:

a[a.length-1]=5;

1
  • This is an ArrayList not an array.
    – glee8e
    Apr 5, 2017 at 3:14
-5

To Get the last value of arraylist in JavaScript :

var yourlist = ["1","2","3"];
var lastvalue = yourlist[yourlist.length -1];

It gives the output as 3 .

2
  • 5 negative comments without giving reason in comment. Aug 29, 2022 at 21:23
  • 1
    Its correct solution. but solution asked for Java
    – Kumaravel
    Nov 23, 2022 at 10:40
-6

Alternative using the Stream API:

list.stream().reduce((first, second) -> second)

Results in an Optional of the last element.

0
-10

In Kotlin, you can use the method last:

val lastItem = list.last()
2
  • 19
    This is java however
    – Jachdich
    Oct 3, 2019 at 10:19
  • 5
    One of the ideas behind creating Kotlin was to cover the little uncomfortable sides of Java. So I think it makes sense to recommend considering Kotlin, at least for the parts of the application that do data analasys. Feb 8, 2020 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.