How to get the first element of the List or Set?

user496949 picture user496949 · Jan 16, 2012 · Viewed 614.1k times · Source

I'd like to know if I can get the first element of a list or set. Which method to use?

Answer

stacker picture stacker · Jan 16, 2012

See the javadoc

of List

list.get(0);

or Set

set.iterator().next();

and check the size before using the above methods by invoking isEmpty()

!list_or_set.isEmpty()