How to get an Object from a Set if you know that there is only one Object in it?

java, set

Solution

Try:

carObject.iterator().next();

Problem

Imagine a code like: ``` Set<Car> carObject; ``` Now I know that there is only one object in this Set. How do I get it properly? ``` Car myCar = carObject.whatMethod ? ``` ??? Edit: Thanks for the great answers. I know there is only one object as I have an ``` if(carObject.size ... ) ``` check just before.

Original source