Java tutorial
//package com.java2s; //License from project: Apache License import java.util.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; public class Main { /** * Recursively iterate the given {@link Collection} and all {@link Collection}s within it. When an element is encountered that equals * the given {@link Object}, the method returns {@code true}. * * @param collection The collection to iterate recursively. * @param o The object to look for in the collection. * * @return {@code true} if the object was found anywhere within the collection or any of its sub-collections. */ public static boolean recurseContains(@Nonnull final Iterable<?> collection, @Nullable final Object o) { for (final Object co : collection) if (co.equals(o) || co instanceof Collection<?> && recurseContains((Iterable<?>) co, o)) return true; return false; } }