Here you can find the source of removeDuplicates(final Object[] array)
Parameter | Description |
---|---|
array | the objects array to be parsed |
public static Object[] removeDuplicates(final Object[] array)
//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; } }