Java Collection Contain contains(Collection c, Object o, Comparator comparator)

Here you can find the source of contains(Collection c, Object o, Comparator comparator)

Description

checks whether the object o is contained in the collection c

License

Open Source License

Parameter

Parameter Description
comparator if not null, used to determine if two items are the same

Declaration

public static boolean contains(Collection c, Object o,
        Comparator comparator) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//w ww  .j a v a 2  s. c om
     * checks whether the object o is contained in the collection c
     *
     * @param comparator if not null, used to determine if two items are the same
     */
    public static boolean contains(Collection c, Object o,
            Comparator comparator) {
        if (comparator == null) {
            // simply check if 'c' contains the pointer 'o'
            return c.contains(o);
        } else {
            // look into 'c' for occurence of 'o'
            for (Object o2 : c) {
                if (comparator.compare(o, o2) == 0) { // the objects match
                    return true;
                }
            }
        }
        return false;
    }
}

Related

  1. contains( C collection, K key)
  2. contains(Collection coll, Object o, Comparator c)
  3. contains(Collection collection, Object object)
  4. contains(Collection objects, Object o)
  5. contains(Collection searchIn, Object[] find)