Java List Null Empty fillUpWithNulls(List list)

Here you can find the source of fillUpWithNulls(List list)

Description

Expands an integer list to a size equal to its value range and adds null-value entries for every missing intermediate integer value.

License

Open Source License

Parameter

Parameter Description
list List containing integer values

Return

An expanded list containing null values for missing intermediate integer values

Declaration

public static List<Integer> fillUpWithNulls(List<Integer> list) 

Method Source Code

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

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    /**/*from   ww  w  . j ava  2 s.c  o  m*/
     * Expands an integer list to a size equal to its value range and adds
     * <code>null</code>-value entries for every missing intermediate integer
     * value.
     * 
     * @param list
     *            List containing integer values
     * @return An expanded list containing null values for missing intermediate
     *         integer values
     */
    public static List<Integer> fillUpWithNulls(List<Integer> list) {
        return fillUpWithNulls(list, null);
    }

    /**
     * Expands an integer list to a size equal to its value range and adds
     * <code>null</code>-value entries for every missing intermediate integer
     * value. If <code>replace</code> is not <code>null</code>, all original
     * values are replaced by <code>replace</code>.
     * 
     * @param list
     *            List containing integer values
     * @param replace
     *            Replacement for existing values within <code>list</code>
     * @return An expanded list containing null values for missing intermediate
     *         integer values and optionally replaced original values
     */
    public static List<Integer> fillUpWithNulls(List<Integer> list, Integer replace) {
        Collections.sort(list);
        int minValue = list.get(0);
        int maxValue = list.get(list.size() - 1);
        int range = (int) (maxValue - Math.signum(minValue) * Math.abs(minValue));

        List<Integer> result = new ArrayList<>(range + 1);
        for (int i = 0; i < list.size() - 1; i++) {
            result.add(replace != null ? replace : list.get(i));
            for (int j = 0; j < list.get(i + 1) - list.get(i) - 1; j++)
                result.add(null);
        }
        result.add(replace != null ? replace : maxValue);
        return result;
    }
}

Related

  1. equalsEmptyEqNull(List list1, List list2)
  2. equalsNullSafe(List list1, List list2)
  3. fillEmptyEntries(Iterable keys, Map> map)
  4. fillNull(final List list, final int numElements)
  5. fillNull(List lst, int size)
  6. getEmptyList(Class cls)
  7. getEmptyList(Class tClass)
  8. getIgnoreNull(List list, int n)
  9. getListIgnoringNulls(T[] items)