Java List Sort getIndexForSortedInsert(final List listSorted, final String item)

Here you can find the source of getIndexForSortedInsert(final List listSorted, final String item)

Description

Determines for a sorted list and an object, what index the string could be inserted.

License

Open Source License

Parameter

Parameter Description
listSorted Sorted string list. Can be <code>null</code>.
item String to be inserted. Can be <code>null</code>.

Return

Index for string insertion.

Declaration

private static int getIndexForSortedInsert(final List<String> listSorted, final String item) 

Method Source Code


//package com.java2s;

import java.util.List;

public class Main {
    /**//  w w  w . ja v  a2 s.  co m
     * Determines for a sorted list and an object, what index the string
     * could be inserted. Note: This method just returns the index, but does not
     * insert the string.
     * 
     * @param listSorted
     *            Sorted string list. Can be <code>null</code>.
     * @param item
     *            String to be inserted. Can be <code>null</code>.
     * 
     * @return Index for string insertion.
     */
    private static int getIndexForSortedInsert(final List<String> listSorted, final String item) {
        if (listSorted == null || item == null) {
            return 0;
        }

        int low = 0;
        int high = listSorted.size() - 1;

        while (low <= high) {
            final int mid = (low + high) >>> 1;
            final String midVal = listSorted.get(mid);
            final int cmp = midVal.compareToIgnoreCase(item);

            if (cmp < 0) {
                low = mid + 1;
            } else if (cmp > 0) {
                high = mid - 1;
            } else {
                return mid; // key found
            }
        }

        return low; // key not found.
    }
}

Related

  1. calculateQ1(List values, boolean copyAndSort)
  2. descendingSortByCreationTime(List tasks)
  3. equalUnsorted(List list1, List list2)
  4. extractRankedProducts( List unsortedProducts)
  5. formatAsSortUniqCSVString(List s)
  6. getNumberListSorted(String pagesStr)
  7. getOverlapCount(List list1, List list2, boolean sorted)
  8. getSortedFilesList(String filesList)
  9. getSortedKetList(HashMap numKnownGenesHashMap)