Java List Sort addSorted(final List list, final T element)

Here you can find the source of addSorted(final List list, final T element)

Description

Adds a given new Comparable element to a given (already sorted) List at the right location so that the sorting order remains valid

License

Apache License

Parameter

Parameter Description
list the List where to insert the new element
element the new element to insert
T the type of elements of the given List

Declaration

public static <T extends Comparable<? super T>> void addSorted(final List<T> list, final T element) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {
    /**//from ww w .j  a  v  a 2  s .  c o  m
     * Adds a given new {@link Comparable} element to a given (already sorted)
     * {@link List} at the right location so that the sorting order remains valid
     * 
     * @param list
     *           the {@link List} where to insert the new element
     * @param element
     *           the new element to insert
     * @param <T>
     *           the type of elements of the given {@link List}
     */
    public static <T extends Comparable<? super T>> void addSorted(final List<T> list, final T element) {
        list.add(getInsertIndex(list, element), element);
    }

    /**
     * Adds a given new element to a given (already sorted) {@link List} at the
     * right location so that the sorting order determined by the given
     * {@link Comparator} remains valid
     * 
     * @param list
     *           the {@link List} where to insert the new element
     * @param element
     *           the new element to insert
     * @param comparator
     *           the {@link Comparator} that determines the sorting order
     * @param <T>
     *           the type of elements of the given {@link List}
     */
    public static <T> void addSorted(final List<T> list, final T element, final Comparator<? super T> comparator) {
        list.add(getInsertIndex(list, element, comparator), element);
    }

    /**
     * Determines for a given (already sorted) {@link List} and a given new
     * {@link Comparable} element the insert index so that the sorting order
     * remains valid
     * 
     * @param list
     *           the {@link List} where to insert the new element
     * @param element
     *           the new element to insert
     * @param <T>
     *           the type of element to get the insert index for
     * @return the respective insert index to keep the sorting order valid
     */
    public static <T> int getInsertIndex(final List<? extends Comparable<? super T>> list, final T element) {
        int index = Collections.binarySearch(list, element);

        if (index < 0) {
            index = -index - 1;
        }

        return index;
    }

    /**
     * Determines for a given (already sorted) {@link List} and a given new
     * element the insert index so that the sorting order determined by the given
     * {@link Comparator} remains valid
     * 
     * @param list
     *           the {@link List} where to insert the new element
     * @param element
     *           the new element to insert
     * @param comparator
     *           the {@link Comparator} that determines the sorting order
     * @param <T>
     *           the type of element to get the insert index for
     * @return the respective insert index to keep the sorting order valid
     */
    public static <T> int getInsertIndex(final List<? extends T> list, final T element,
            final Comparator<? super T> comparator) {
        int index = Collections.binarySearch(list, element, comparator);

        if (index < 0) {
            index = -index - 1;
        }

        return index;
    }
}

Related

  1. addAllSortNoDuplicates(List l1, List l2)
  2. calculateBox(List values, boolean copyAndSort)
  3. calculateQ1(List values, boolean copyAndSort)
  4. descendingSortByCreationTime(List tasks)
  5. equalUnsorted(List list1, List list2)