Java Collection Equal containsByEquals(Collection collection, Object obj)

Here you can find the source of containsByEquals(Collection collection, Object obj)

Description

contains By Equals

License

LGPL

Declaration

public static int containsByEquals(Collection collection, Object obj) 

Method Source Code

//package com.java2s;
/*//w ww  .ja v a 2  s .c  om
Strandz LGPL - an API that matches the user to the data.
Copyright (C) 2007 Chris Murphy
    
Strandz LGPL is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
    
The authors can be contacted via www.strandz.org
*/

import java.util.Collection;

import java.util.Iterator;

public class Main {
    public static int containsByEquals(Collection collection, Object obj) {
        int result = 0;
        for (Iterator iter = collection.iterator(); iter.hasNext();) {
            Object element = iter.next();
            if (element.equals(obj)) {
                result++;
            }
        }
        return result;
    }

    /**
     * Nearly the same as contains for a list, but works for an array.
     */
    public static int containsByEquals(Object[] comps, Object obj) {
        int result = 0;
        for (int i = 0; i < comps.length; i++) {
            Object element = comps[i];
            if (element.equals(obj)) {
                result++;
            }
        }
        return result;
    }

    /**
     * Saves the hassle of doing the checking for null yourself.
     *
     * @param obj1
     * @param obj2
     */
    public static boolean equals(Object obj1, Object obj2) {
        boolean result = false;
        if (obj1 == null && obj2 == null) {
            result = true;
        } else if (obj1 == null || obj2 == null) {
        } else {
            result = obj1.equals(obj2);
        }
        return result;
    }

    public static boolean equals(boolean b1, boolean b2) {
        return b1 == b2;
    }

    public static boolean equals(int i1, int i2) {
        return i1 == i2;
    }
}

Related

  1. allEqual(final Class[] collection, final Class cls)
  2. areEqual(Collection collection1, Collection collection2)
  3. areEqual(Collection collection1, Collection collection2)
  4. compareTwoCollectionsEquality(Collection expectedAffectedFiles, Collection affectedFiles)
  5. containsAtleastOneEqualString(Collection left, String right)
  6. content_equality(Collection A, Collection B)
  7. contentEquals(Collection l1, Collection l2)
  8. contentEquals(Collection list1, Collection list2)
  9. equal(Collection i1, Collection i2)