I used Collections.sort(playersList);
to sort a List
. So, I think playersList
is sorted now. But how can I get the first element of the list? playersList[0]
does not work.
-
Update: In Java 21+, use `getFirst` as shown in [this Answer](https://stackoverflow.com/a/76989307/642706). – Basil Bourque Sep 01 '23 at 23:18
9 Answers
playersList.get(0)
Java has limited operator polymorphism. So you use the get()
method on List
objects, not the array index operator ([]
)

- 278,309
- 50
- 514
- 539
You have to access lists a little differently than arrays in Java. See the javadocs for the List
interface for more information.
playersList.get(0)
However if you want to find the smallest element in playersList
, you shouldn't sort it and then get the first element. This runs very slowly compared to just searching once through the list to find the smallest element.
For example:
int smallestIndex = 0;
for (int i = 1; i < playersList.size(); i++) {
if (playersList.get(i) < playersList.get(smallestIndex))
smallestIndex = i;
}
playersList.get(smallestIndex);
The above code will find the smallest element in O(n)
instead of O(n log n)
time.

- 136,852
- 53
- 295
- 323
-
1I wander why such a basic operation should be programed. Why Java does not provide a function which returns just a minimal value from array? – Roman Mar 24 '10 at 08:58
-
3Roman, there is such a method, Collections.min (http://java.sun.com/javase/7/docs/api/java/util/Collections.html#min%28java.util.Collection%29). – Matthew Flaschen Mar 24 '10 at 15:10
That depends on what type your list is, for ArrayList
use:
list.get(0);
for LinkedList
use:
list.getFirst();
if you like the array
approach:
list.toArray()[0];

- 23,135
- 6
- 55
- 69
-
2bad advice. LinkedList implements the List interface, no need to use a special method (and I'd be amazed of there was any performance difference between the two). And calling toArray() is wasteful--you might be allocating the list into a new array for no reason! – Kip Mar 23 '10 at 20:57
-
@Kip, strange remark; the LinkedList class does not implement the first and last methods out of spite. If you have a good reason to use a LinkedList, you should not refrain from using its methods just because they are not in the List interface. The array example can be usefull if the list itself is not needed after sorting, and closest to what OP asked. Without knowing the context of the source code in question you cannot determine the validity of the advice. – rsp Mar 23 '10 at 21:44
-
why would it matter whether or not you need the list after sorting? in either case (or even if the list is never sorted) calling toArray() just to get the first element (may) needlessly create an entire array. – Kip Mar 24 '10 at 03:18
-
1@Kip, you are right in that retrieving an array just for the first element is ot a good idea if you don not plan to use the array. The question asked for how to get the first element from a list and mentioned an array context. I merely gave alternatives touching those concepts. It is up to the programmer who knows his source to make a choice from these alternatives. – rsp Mar 24 '10 at 08:54
Using Java 8 streams, you can turn your list into a stream and get the first item in a list using the .findFirst()
method.
List<String> stringsList = Arrays.asList("zordon", "alpha", "tommy");
Optional<String> optional = stringsList.stream().findFirst();
optional.get(); // "zordon"
The .findFirst()
method will return an Optional that may or may not contain a string value (it may not contain a value if the stringsList
is empty).
Then to unwrap the item from the Optional use the .get()
method.

- 9,477
- 4
- 61
- 77
Since Java 21, simply using List.getFirst
:
System.out.println(playersList.getFirst());
Note: Throws NoSuchElementException
if the list is empty.
Additional details in JEP 431: Sequenced Collections.

- 14,685
- 6
- 61
- 90
Matthew's answer is correct:
list.get(0);
To do what you tried:
list[0];
you'll have to wait until Java 7 is released:
devoxx conference http://img718.imageshack.us/img718/11/capturadepantalla201003cg.png
Here's an interesting presentation by Mark Reinhold about Java 7
It looks like parleys site is currently down, try later :(
If your collection is not a List
(and thus you can't use get(int index)
), then you can use the iterator:
Iterator iter = collection.iterator();
if (iter.hasNext()) {
Object first = iter.next();
}

- 61,876
- 75
- 195
- 257
If you just want to get the minimum of a list, instead of sorting it and then getting the first element (O(N log N)
), you can use do it in linear time using min
:
<T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)
That looks gnarly at first, but looking at your previous questions, you have a List<String>
. In short: min
works on it.
For the long answer: all that super
and extends
stuff in the generic type constraints is what Josh Bloch calls the PECS principle (usually presented next to a picture of Arnold -- I'M NOT KIDDING!)
Producer Extends, Consumer Super
It essentially makes generics more powerful, since the constraints are more flexible while still preserving type safety (see: what is the difference between ‘super’ and ‘extends’ in Java Generics)

- 1
- 1

- 376,812
- 128
- 561
- 623
public class Main {
public static List<String> list = new ArrayList();
public static void main(String[] args) {
List<Integer> l = new ArrayList<>();
l.add(222);
l.add(100);
l.add(45);
l.add(415);
l.add(311);
l.sort(null);
System.out.println(l.get(0));
}
}
without l.sort(null) returned 222
with l.sort(null) returned 45

- 1,056
- 10
- 13