Java Array Unique getUniqueIDsArray(int[] idArray, String ids, String seperator)

Here you can find the source of getUniqueIDsArray(int[] idArray, String ids, String seperator)

Description

get Unique I Ds Array

License

Open Source License

Declaration

public static int[] getUniqueIDsArray(int[] idArray, String ids, String seperator) 

Method Source Code


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

import java.util.*;

public class Main {

    public static int[] getUniqueIDsArray(int[] idArray, String ids, String seperator) {
        Set<Integer> idSet = new HashSet<Integer>();
        if (idArray != null) {
            for (int id : idArray) {
                idSet.add(id);// www .  j a v  a  2  s . c  o  m
            }
        }
        int[] tmpIdArray = transIDsToArray(ids, seperator);
        if (tmpIdArray != null) {
            for (int id : tmpIdArray) {
                idSet.add(id);
            }
        }

        int[] targetArray = new int[idSet.size()];
        int idx = 0;

        Iterator<Integer> iterator = idSet.iterator();
        while (iterator.hasNext()) {
            targetArray[idx] = iterator.next();
            idx++;
        }
        return targetArray;
    }

    public static int[] transIDsToArray(String ids, String seprator) {
        if (ids == null || seprator == null) {
            return null;
        }
        String[] tmpArray = ids.split(seprator);

        List<String> validList = new ArrayList<String>();
        for (int i = 0; i < tmpArray.length; i++) {
            if (tmpArray[i].matches("^\\d+$")) {
                validList.add(tmpArray[i]);
            }
        }

        int[] targetArray = new int[validList.size()];
        for (int i = 0; i < validList.size(); i++) {
            targetArray[i] = Integer.valueOf(validList.get(i));
        }
        return targetArray;
    }
}

Related

  1. countUnique(int[][] array)
  2. getUniqueItems(final String[] allItems, final String newItem, final int maxItems)
  3. getUniqueWords(String[] input)
  4. invertRelabeling(int[] relabeling, int[] uniqueVars, int maxVarNum)
  5. unique(double[] in)