Here you can find the source of getFirst(Collection
private static <T> T getFirst(Collection<T> set, boolean remove)
//package com.java2s; import java.util.Collection; import java.util.Iterator; public class Main { private static <T> T getFirst(Collection<T> set, boolean remove) { Iterator<T> iterator = set.iterator(); if (!iterator.hasNext()) { throw new IllegalStateException("The set is empty"); }/*from w ww .j a va 2 s.co m*/ T first = iterator.next(); if (remove) { iterator.remove(); } return first; } }