I have the following kind of situation:

Set<Element> set = getSetFromSomewhere();
if (set.size() == 1) {
    // return the only element
} else {
    throw new Exception("Something is not right..");
}

Assuming I cannot change the return type of getSetFromSomewhere(), is there a better or more correct way to return the only element in the set than

  • Iterating over the set and returning immediately
  • Creating a list from the set and calling .get(0)
Edit
Report