Example usage for java.util List set

List of usage examples for java.util List set

Introduction

In this page you can find the example usage for java.util List set.

Prototype

E set(int index, E element);

Source Link

Document

Replaces the element at the specified position in this list with the specified element (optional operation).

Usage

From source file:Main.java

private static <T> void sortRecursive(List<T> list, List<T> unsorted, Comparator<? super T> comp, int begin,
        int end) {
    //A sorting section with no elements or one element needs not be sorted
    if (begin == end || begin == end - 1) {
        return;/*w  w  w  .  j  av a  2 s .  c  o  m*/
    }
    //A sorting section with two elements can be sorted trivially
    //Swap if the larger element precedes the smaller
    if (begin == end - 2) {
        T a = list.get(begin);
        T b = list.get(begin + 1);
        if (comp.compare(a, b) > 0) {
            list.set(begin, b);
            list.set(begin + 1, a);
        }
        return;
    }
    //Split the array into half
    int split = (end + begin) / 2;
    //Sort each half of the array
    sortRecursive(list, unsorted, comp, begin, split);
    sortRecursive(list, unsorted, comp, split, end);
    int indexCounter = 0;
    int counterLeft = begin;
    int counterRight = split;
    boolean complete = false;
    //Merging the arrays
    //Fills in the indexes; one plots the final position of
    //the element to the element's index; the other plots the
    //element at the given index to its final position
    while (!complete) {
        T a = unsorted.get(counterLeft);
        T b = unsorted.get(counterRight);
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            list.set(begin + indexCounter++, b);
            counterRight++;
            if (counterRight >= end) {
                //Copy the remaining values
                while (counterLeft < split) {
                    list.set(begin + indexCounter++, unsorted.get(counterLeft++));
                }
                complete = true;
            }
        } else {
            list.set(begin + indexCounter++, a);
            counterLeft++;
            if (counterLeft >= split) {
                //Copy the remaining values
                while (counterRight < end) {
                    list.set(begin + indexCounter++, unsorted.get(counterRight++));
                }
                complete = true;
            }
        }
    }
    //The list should now be sorted
}

From source file:com.fegati.utilities.CSVPreprocessor.java

private static void titleHeader(List<String[]> rows) {

    int numberOfUntitledColumn = 0;

    if (rows.size() > 0) {
        String[] header = rows.get(HEADER_INDEX);

        for (int i = 0; i < header.length; i++) {
            /* Add default title if the column is an empty string */
            if (header[i].equals(DEFAULT_EMPTY_VALUE)) {
                header[i] = DEFAULT_TITLE_PREFIX + String.valueOf(numberOfUntitledColumn++);
            }//  w  ww . j  a v a 2s .com
        }
        rows.set(HEADER_INDEX, header);
    }
}

From source file:Main.java

private static <T> void sortRecursive(List<T> array, Comparator<? super T> comp, int begin, int end) {
    //A sorting section with no elements or one element needs not be sorted
    if (begin == end || begin == end - 1) {
        return;//from w w  w.  j  a v a 2  s.  co  m
    }
    //A sorting section with two elements can be sorted trivially
    //Swap if the larger element precedes the smaller
    if (begin == end - 2) {
        T a = array.get(begin);
        T b = array.get(begin + 1);
        if (comp.compare(a, b) > 0) {
            array.set(begin, b);
            array.set(begin + 1, a);
        }
        return;
    }
    //Split the array into half
    int split = (end + begin) / 2;
    //Sort each half of the array
    sortRecursive(array, comp, begin, split);
    sortRecursive(array, comp, split, end);
    //Create indexes for storing sorting data
    int[] indexA = new int[end - begin];
    int[] indexB = new int[end - begin];
    int indexCounter = 0;
    int counterLeft = begin;
    int counterRight = split;
    boolean complete = false;
    //Merging the arrays
    //Fills in the indexes; one plots the final position of
    //the element to the element's index; the other plots the
    //element at the given index to its final position
    while (!complete) {
        T a = array.get(counterLeft);
        T b = array.get(counterRight);
        //Compares values, then adds the smaller
        //one to the indexes
        if (comp.compare(a, b) > 0) {
            indexA[indexCounter] = counterRight - begin;
            indexB[counterRight++ - begin] = indexCounter++;
            if (counterRight >= end) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterLeft < split) {
                    indexA[indexCounter] = counterLeft - begin;
                    indexB[counterLeft++ - begin] = indexCounter++;
                }
                complete = true;
            }
        } else {
            indexA[indexCounter] = counterLeft - begin;
            indexB[counterLeft++ - begin] = indexCounter++;
            if (counterLeft >= split) {
                //When one array is complete,
                //add the remainder of the other to the index
                while (counterRight < end) {
                    indexA[indexCounter] = counterRight - begin;
                    indexB[counterRight++ - begin] = indexCounter++;
                }
                complete = true;
            }
        }
    }
    //Swaps the elements in the array such that the array is sorted
    for (int i = 0; i < end - begin; i++) {
        int target = indexA[i];
        T a = array.get(begin + target);
        T b = array.get(begin + i);
        array.set(begin + target, b);
        array.set(begin + i, a);
        int ai = indexB[target];
        int bi = indexB[i];
        indexB[target] = bi;
        indexB[i] = ai;
        indexA[bi] = target;
    }
    //The array should now be sorted
}

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

protected static <T> void resetList(List<T> oldValues, List<T> newValues) {
    if (newValues == null) {
        if (oldValues != null) {
            oldValues.clear();/*from  w  ww  .  ja  va 2s.  c  o  m*/
        }
        return;
    }
    Iterator<T> iterator = newValues.iterator();
    for (int i = 0; iterator.hasNext(); i++) {
        T nextNew = iterator.next();
        if (nextNew != null) {
            if (i < oldValues.size() && !nextNew.equals(oldValues.get(i))) {
                oldValues.set(i, nextNew);
            } else if (i >= oldValues.size()) {
                oldValues.add(i, nextNew);
            }
        }
    }
    while (oldValues.size() > newValues.size()) {
        oldValues.remove(oldValues.size() - 1);
    }
}

From source file:Main.java

/**
 * Move the selected items down one position in the list.
 * /*  ww w.  ja  v  a2 s .  com*/
 * @param list
 *            The list of items, which will be altered in-place.
 * @param selectionIndexes
 *            The indexes of the items to be moved.
 * @return Whether any items have been moved. For example, would return false if the selected items were already at
 *         the bottom of the list.
 */
public static <T> boolean moveDown(List<T> list, int[] selectionIndexes) {
    boolean moved = false;

    int size = list.size();
    for (int i = 0; i < size; i++) {
        T item = list.get(i);
        if (arrayContains(selectionIndexes, i)) {
            // Find next unselected item
            int nextUnselected = -1;
            int j = i + 1;
            while (j < size) {
                if (!arrayContains(selectionIndexes, j)) {
                    nextUnselected = j;
                    break;
                }
                j++;
            }
            // Swap with it
            if (nextUnselected != -1) {
                list.set(i, list.get(nextUnselected));
                list.set(nextUnselected, item);
                moved = true;
            }
        }
    }
    return moved;
}

From source file:Main.java

/**
 * Move the selected items up one position in the list.
 * //w w w.j ava2  s . com
 * @param list
 *            The list of items, which will be altered in-place.
 * @param selectionIndexes
 *            The indexes of the items to be moved.
 * @return Whether any items have been moved. For example, would return false if the selected items were already at
 *         the top of the list.
 */
public static <T> boolean moveUp(List<T> list, int[] selectionIndexes) {
    boolean moved = false;

    int size = list.size();
    for (int i = size - 1; i >= 0; i--) {
        T item = list.get(i);
        if (arrayContains(selectionIndexes, i)) {
            // Find next unselected item
            int nextUnselected = -1;
            int j = i - 1;
            while (j >= 0) {
                if (!arrayContains(selectionIndexes, j)) {
                    nextUnselected = j;
                    break;
                }
                j--;
            }
            // Swap with it
            if (nextUnselected != -1) {
                list.set(i, list.get(nextUnselected));
                list.set(nextUnselected, item);
                moved = true;
            }
        }
    }
    return moved;
}

From source file:acromusashi.stream.ml.clustering.kmeans.KmeansCalculator.java

/**
 * ?Counts??//from   ww w . j  a  v a2  s  .c  om
 * 
 * @param baseCounts Counts
 * @param targetCounts Counts
 * @param resultMapping ??
 * @return ?Counts
 */
protected static List<Long> mergeCounts(List<Long> baseCounts, List<Long> targetCounts,
        Map<Integer, Integer> resultMapping) {
    int countNum = resultMapping.size();
    List<Long> mergedCounts = new ArrayList<>(countNum);
    for (int count = 0; count < countNum; count++) {
        mergedCounts.add(0L);
    }

    for (Entry<Integer, Integer> resultEntry : resultMapping.entrySet()) {
        mergedCounts.set(resultEntry.getKey(),
                baseCounts.get(resultEntry.getKey()) + targetCounts.get(resultEntry.getValue()));
    }

    return mergedCounts;
}

From source file:net.sf.jabref.logic.util.io.FileUtil.java

/**
 * Creates the minimal unique path substring for each file among multiple file paths.
 *
 * @param paths the file paths//from w  w  w  .  j  ava  2s  .c o m
 * @return the minimal unique path substring for each file path
 */
public static List<String> uniquePathSubstrings(List<String> paths) {
    List<Stack<String>> stackList = new ArrayList<>(paths.size());
    // prepare data structures
    for (String path : paths) {
        List<String> directories = Arrays.asList(path.split(Pattern.quote(File.separator)));
        Stack<String> stack = new Stack<>();
        stack.addAll(directories);
        stackList.add(stack);
    }

    List<String> pathSubstrings = new ArrayList<>(Collections.nCopies(paths.size(), ""));

    // compute shortest folder substrings
    while (!stackList.stream().allMatch(Vector::isEmpty)) {
        for (int i = 0; i < stackList.size(); i++) {
            String tempString = pathSubstrings.get(i);

            if (tempString.isEmpty() && !stackList.get(i).isEmpty()) {
                pathSubstrings.set(i, stackList.get(i).pop());
            } else if (!stackList.get(i).isEmpty()) {
                pathSubstrings.set(i, stackList.get(i).pop() + File.separator + tempString);
            }
        }

        for (int i = 0; i < stackList.size(); i++) {
            String tempString = pathSubstrings.get(i);

            if (Collections.frequency(pathSubstrings, tempString) == 1) {
                stackList.get(i).clear();
            }
        }
    }
    return pathSubstrings;
}

From source file:com.globalsight.everest.integration.ling.tm2.LeverageMatch.java

public static void orderMatchResult(List list) {
    for (int i = 0; i < list.size() - 1; i++) {
        LeverageMatch lm1 = (LeverageMatch) list.get(i);

        for (int j = i + 1; j < list.size(); j++) {
            LeverageMatch lm2 = (LeverageMatch) list.get(j);

            if (lm1.getScoreNum() < lm2.getScoreNum()) {
                list.set(i, lm2);
                list.set(j, lm1);//from w  w w  . j  a v  a2s.c  o m
                lm1 = lm2;
            }
        }
    }
}

From source file:cn.fql.utility.CollectionUtility.java

/**
 * check each element of first list, and compare it to each element of second list.
 * element of list is <code>java.util.Map</code>, get value from map by specified key, if value
 * of element of first list are equals second's, invoke <code>java.util.Map#putAll</code>
 * of element of first list//from ww  w. j  a  v  a  2 s.  co  m
 *
 * @param first  first collection
 * @param second second collection
 * @param key    specified key
 * @return first list
 */
public static List joinRecords(List first, List second, String key) {
    if (first != null && second != null) {
        for (int i = 0; i < first.size(); i++) {
            Map record1 = (Map) first.get(i);
            for (Iterator iterator1 = second.iterator(); iterator1.hasNext();) {
                Map record2 = (Map) iterator1.next();
                if (record2.get(key) != null && record2.get(key).equals(record1.get(key))) {
                    record1.putAll(record2);
                    break;
                }
            }
            first.set(i, record1);
        }
    }
    return first;
}