Here you can find the source of getIndexForSortedInsert(final List
Parameter | Description |
---|---|
listSorted | Sorted string list. Can be <code>null</code>. |
item | String to be inserted. Can be <code>null</code>. |
private static int getIndexForSortedInsert(final List<String> listSorted, final String item)
//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. } }