Java Array Duplicate removeDuplicates(final Object[] array)

Here you can find the source of removeDuplicates(final Object[] array)

Description

Remove duplicate objects from an array

License

Open Source License

Parameter

Parameter Description
array the objects array to be parsed

Return

an array of objects

Declaration

public static Object[] removeDuplicates(final Object[] array) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2013/* www  .j  a  va  2s  .co  m*/
 * Contributors: L. Armanet, M. Camerlenghi, L. Cardonne, S. Delageniere,
 *               L. Duparchy, S. Ohlsson, P. Pascal, I. Schneider, S.Schulze,
 *               F. Torres
 * 
 * This file is part of the MIS tools package.
 * 
 * The MIS tools package 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 3 of the License, or
 * (at your option) any later version.
 * 
 * The MIS tools package 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 the MIS tools package.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;

public class Main {
    /**
     * Remove duplicate objects from an array
     * 
     * @param array
     *            the objects array to be parsed
     * @return an array of objects
     */
    public static Object[] removeDuplicates(final Object[] array) {
        if (isNull(array)) {
            return null;
        }
        try {
            List listNewPks = new ArrayList();
            listNewPks.addAll(new LinkedHashSet(Arrays.asList(array)));
            return listNewPks.toArray();
        } catch (Exception e) {
            return array;
        }
    }

    /**
     * Checks if an array is null or empty or all its elements are null.
     * 
     * @param array
     * @return true if array is null, empty or contains only null elements
     */
    public static boolean isNull(final Object[] array) {
        if (array == null || array.length == 0) {
            return true;
        }
        for (int i = 0; i < array.length; i++) {
            if (array[i] != null) {
                return false;
            }
        }
        return true;
    }

    /**
     * Checks if a collection is null or empty or all its elements are null.
     * 
     * @param col
     * @return true if collection is null, empty or contains only null elements
     */
    public static boolean isNull(final Collection col) {
        if (col == null || col.isEmpty()) {
            return true;
        }
        Iterator it = col.iterator();
        while (it.hasNext()) {
            if (it.next() != null) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. duplicateArrayEntries(String[] inputArr, int numDuplicates)
  2. eraseDuplicatedValue(String[] srcArr)
  3. hasDuplicates(final T[] array)
  4. isDuplicated(String[] strArray)
  5. removeDuplicates(double[] array)
  6. removeDuplicates(int[] input)
  7. removeDuplicates(T[] elements)
  8. removeDuplicatesInPath(int[][] path)
  9. removeDuplicateStrings(String[] array)