Tests to see if a Collection contains a certain object, given a user specified equality comparator - Android java.util

Android examples for java.util:Collection Contains

Description

Tests to see if a Collection contains a certain object, given a user specified equality comparator

Demo Code


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.apache.log4j.*;

public class Main{
    public static void main(String[] argv){
        Iterator   it = java.util.Arrays.asList("asdf","book2s.com").iterator();
        Object o = "book2s.com";
        System.out.println(contains(it,o));
    }//  w w w. ja  v a2  s  .  c  o m
    /**
     * Tests to see if a vector contains a certain object, given a user
     * specified equality comparator
     * 
     * @param vector
     *            a vector
     * @param o
     *            the object to find
     * @param cmp
     *            the comparitor to use for elements of the vector
     * @return whether vector contains an element that is equal to o
     */
    public static boolean contains(Collection vector, Object o,
            EqualityComparator cmp) {
        Iterator it = vector.iterator();
        while (it.hasNext())
            if (cmp.equals(it.next(), o))
                return true;
        return false;
    }
    public static boolean contains(Iterator it, Object o) {
        while (it.hasNext()) {
            if (ObjectUtil.equals(o, it.next()))
                return true;
        }
        return false;
    }
}

Related Tutorials