CollectionsUtil { public static <T> T getAnyFrom(Collection<T> collection) { final Iterator<T> iterator = collection.iterator(); if (iterator.hasNext()) { return iterator.next(); } throw new NoSuchElementException("No value present"); } private  CollectionsUtil(); static Optional<T> findAnyIn(Collection<T> collection); static T getAnyFrom(Collection<T> collection);  }
@Test void getAnyFrom() { final List<String> populatedList = Collections.singletonList("value"); assertEquals("value", CollectionsUtil.getAnyFrom(populatedList)); final List<String> emptyList = Collections.emptyList(); assertThrows(NoSuchElementException.class, () -> CollectionsUtil.getAnyFrom(emptyList)); }