Java List Count countOccurrencesOf(Object comparisonObject, List list)

Here you can find the source of countOccurrencesOf(Object comparisonObject, List list)

Description

Return an integer representing the number of occurrences (using equals()) of the specified object in the specified list.

License

Open Source License

Declaration

public static int countOccurrencesOf(Object comparisonObject, List list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.//from   w w  w.  ja va2 s . co m
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation from Oracle TopLink
 *     dminsky - added countOccurrencesOf(Object, List) API
 *     08/23/2010-2.2 Michael O'Brien
 *        - 323043: application.xml module ordering may cause weaving not to occur causing an NPE.
 *                       warn if expected "_persistence_*_vh" method not found
 *                       instead of throwing NPE during deploy validation.
 ******************************************************************************/

import java.util.List;

public class Main {
    /**
     * Return an integer representing the number of occurrences (using equals()) of the
     * specified object in the specified list.
     * If the list is null or empty (or both the object and the list is null), 0 is returned.
     */
    public static int countOccurrencesOf(Object comparisonObject, List list) {
        int instances = 0;
        boolean comparisonObjectIsNull = comparisonObject == null;
        if (list != null) {
            for (int i = 0; i < list.size(); i++) {
                Object listObject = list.get(i);
                if ((comparisonObjectIsNull & listObject == null)
                        || (!comparisonObjectIsNull && comparisonObject
                                .equals(listObject))) {
                    instances++;
                }
            }
        }
        return instances;
    }
}

Related

  1. count(List lines)
  2. countAtLevel(List aList, int aLevel)
  3. countNumber(List list, int number)
  4. countOccurances(List encodings, int value)
  5. countOccurence(List valueList, int value)