Is there any equivalent to Collections.getOnlyElement() that works with arrays?
I’m aware it’s a trivial function to implement, but Collections.getOnlyElement() is as well, and it’s in guava.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Iterators.getOnlyElement(Iterators.forArray(array))By using
Iterators.forArray, you can bypass the need to create a copy of this array as a list, and instead iterate over the array itself. Then useIterators.getOnlyElementto get the first element from an iterator.