How can I get a first element from a sorted list?

132,431

Solution 1

playersList.get(0)

Java has limited operator polymorphism. So you use the get() method on List objects, not the array index operator ([])

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 5

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 :(

Share:
132,431
Roman
Author by

Roman

Updated on May 07, 2020

Comments

  • Roman
    Roman almost 4 years

    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.

  • Kip
    Kip about 14 years
    bad 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!
  • rsp
    rsp about 14 years
    @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.
  • Kip
    Kip about 14 years
    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.
  • rsp
    rsp about 14 years
    @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.
  • Roman
    Roman about 14 years
    I wander why such a basic operation should be programed. Why Java does not provide a function which returns just a minimal value from array?
  • Matthew Flaschen
    Matthew Flaschen about 14 years
    Roman, there is such a method, Collections.min (java.sun.com/javase/7/docs/api/java/util/…).
  • Gunner
    Gunner about 12 years
    This doesn't seem to have actually made it into java 7