Java tutorial
//package com.java2s; //License from project: Creative Commons License import java.util.HashSet; import java.util.Optional; import java.util.function.Predicate; public class Main { /** * Returns item in HashSet that matches the specified Predicate. * @param set the HashSet to check against * @param condition the Predicate to check * @param <T> the type of objects in the HashSet * @return an Optional containing the matching object, Empty if not found * @throws IllegalArgumentException if HashSet or Predicate is null */ public static <T> Optional<T> getItemInHashSet(final HashSet<T> set, final Predicate<T> condition) { if (set == null) throw new IllegalArgumentException("HashSet cannot be null!"); if (condition == null) throw new IllegalArgumentException("Predicate cannot be null!"); for (final T object : set) if (condition.test(object)) return Optional.of(object); return Optional.empty(); } }