通过给定Iterator检查是否包含给定元素

import java.util.Enumeration;
import java.util.Iterator;

public class IteratorContains {

/**
 * Check whether the given Iterator contains the given element.
 *
 * @param iterator the Iterator to check
 * @param element  the element to look for
 * @return {@code true} if found, {@code false} else
 */
public static boolean contains(Iterator iterator, Object element) {
    if (iterator != null) {
        while (iterator.hasNext()) {
            Object candidate = iterator.next();
            if (org.springframework.util.ObjectUtils.nullSafeEquals(
                    candidate, element)) {
                return true;
            }
        }
    }
    return false;
}

/**
 * Check whether the given Enumeration contains the given element.
 *
 * @param enumeration the Enumeration to check
 * @param element     the element to look for
 * @return {@code true} if found, {@code false} else
 */
public static boolean contains(Enumeration enumeration,
        Object element) {
    if (enumeration != null) {
        while (enumeration.hasMoreElements()) {
            Object candidate = enumeration.nextElement();
            if (org.springframework.util.ObjectUtils.nullSafeEquals(
                    candidate, element)) {
                return true;
            }
        }
    }
    return false;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Iterator iterator = java.util.Arrays.asList("asdf", "java")
            .iterator();
    Object element = "java";
    System.out.println(contains(iterator, element));
}

}

你可能感兴趣的:(通过给定Iterator检查是否包含给定元素)