Java List Sub List getIndexesId(List aList, List aSubList)

Here you can find the source of getIndexesId(List aList, List aSubList)

Description

Returns an array of indexes for given list and given objects in list.

License

Open Source License

Declaration

public static int[] getIndexesId(List aList, List aSubList) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//from   ww w .  ja  v  a2  s.  c  o  m
     * Returns an array of indexes for given list and given objects in list.
     */
    public static int[] getIndexesId(List aList, List aSubList) {
        // Get list of unique indexes for given list and sublist
        List<Integer> indexes = new ArrayList();
        for (Object obj : aSubList) {
            int index = indexOfId(aList, obj);
            if (!indexes.contains(index))
                indexes.add(index);
        }

        // Get indexes as int array and return
        int indxs[] = new int[indexes.size()];
        for (int i = 0, iMax = indexes.size(); i < iMax; i++)
            indxs[i] = indexes.get(i);
        return indxs;
    }

    /**
     * Returns index of identical given object in given list.
     */
    public static int indexOfId(List aList, Object anObj) {
        // Iterate over list objects and return index if identical exists
        for (int i = 0, iMax = size(aList); i < iMax; i++)
            if (anObj == aList.get(i))
                return i;

        // Return -1 if identical doesn't exist
        return -1;
    }

    /**
     * Returns whether list contains given object (accepts null list).
     */
    public static boolean contains(List aList, Object anObj) {
        return aList != null && aList.contains(anObj);
    }

    /**
     * Adds an object to the given list and returns list (creates list if missing).
     */
    public static <T> List<T> add(List<T> aList, T anObj) {
        // If list is null, create list
        if (aList == null)
            aList = new Vector();

        // Add object
        aList.add(anObj);

        // Return list
        return aList;
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }
}

Related

  1. copySubList(final List list, final int fromIndex, final int toIndex)
  2. copySubList(List list, int fromIndex, int toIndex)
  3. divideListInSublistsOfNSize(List list, int n)
  4. getIndicesOfItems(List superList, List sublist)
  5. getRandomSubList(List inputList, double percentage)
  6. getSubList(int offset, int count, List originalList)
  7. getSubList(List entireList, Class cls)