List of usage examples for java.util Collections binarySearch
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
From source file:Main.java
public static void main(String args[]) { List<Character> ll = new LinkedList<Character>(); for (char n = 'A'; n <= 'Z'; n++) ll.add(n);// ww w . j av a2 s .c om Collections.shuffle(ll); for (Character x : ll) System.out.print(x + " "); Collections.sort(ll); for (Character x : ll) System.out.print(x + " "); System.out.println("Searching for F."); int i = Collections.binarySearch(ll, 'F'); if (i >= 0) { System.out.println("Found at index " + i); System.out.println("Object is " + ll.get(i)); } }
From source file:Main.java
public static void main(String[] args) { List list = new LinkedList(); DateFormatSymbols dfs = new DateFormatSymbols(); String[] months = dfs.getMonths(); for (int i = 0; i < months.length; i++) { String month = months[i]; list.add(month);//from ww w . j av a 2s .c o m } Collections.sort(list); System.out.println("Month Names = " + list); int index = Collections.binarySearch(list, "October"); if (index > 0) { System.out.println("Found at index = " + index); String month = (String) list.get(index); System.out.println("Month = " + month); } }
From source file:MainClass.java
public static void main(String args[]) { String simpsons[] = { "B", "H", "L", "M", "H", "M", "R" }; List list = new ArrayList(Arrays.asList(simpsons)); // Ensure list sorted Collections.sort(list);//from ww w .j a v a 2 s. c o m System.out.println("Sorted list: [length: " + list.size() + "]"); System.out.println(list); // Search for element in list int index = Collections.binarySearch(list, "M"); System.out.println("Found M @ " + index); // Search for element not in list index = Collections.binarySearch(list, "J"); System.out.println("Didn't find J @ " + index); }
From source file:EmpComparator.java
public static void main(String args[]) { String names[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" }; // Convert to list List list = new ArrayList(Arrays.asList(names)); // Ensure list sorted Collections.sort(list);//from www. j a v a2 s .c o m System.out.println("Sorted list: [length: " + list.size() + "]"); System.out.println(list); // Search for element in list int index = Collections.binarySearch(list, "Maggie"); System.out.println("Found Maggie @ " + index); // Search for element not in list index = Collections.binarySearch(list, "Jimbo Jones"); System.out.println("Didn't find Jimbo Jones @ " + index); // Insert int newIndex = -index - 1; list.add(newIndex, "Jimbo Jones"); System.out.println("With Jimbo Jones added: [length: " + list.size() + "]"); System.out.println(list); // Min should be Bart System.out.println(Collections.min(list)); // Max should be Roy System.out.println(Collections.max(list)); Comparator comp = Collections.reverseOrder(); // Reversed Min should be Roy System.out.println(Collections.min(list, comp)); // Reversed Max should be Bart System.out.println(Collections.max(list, comp)); }
From source file:Main.java
public static boolean isContain(List<Integer> list, Integer i) { return !isCollectionEmpty(list) && i != null && Collections.binarySearch(list, i) >= 0; }
From source file:Main.java
public static <T> T search(SortedSet<T> set, T element) { T o = null;//from w w w . jav a2s .c o m List l = new ArrayList(set); int index = Collections.binarySearch(l, element); if (index >= 0) { o = (T) l.get(index); } return o; }
From source file:Main.java
public static int binarySearchIndex(Collection<?> collection, Object value) { if ((value instanceof Comparable)) { List valueList;// ww w. ja va 2s. c o m if ((collection instanceof List)) { valueList = (List) collection; } else { valueList = new ArrayList(); valueList.addAll(collection); } return Collections.binarySearch(valueList, value); } return searchIndex(collection, value); }
From source file:Main.java
public static <T extends Comparable<T>> int getLowestInsertionPoint(final List<T> list, final T key) { int index = Collections.binarySearch(list, key); if (index >= 0x00) { return index; } else {//from w w w . ja v a 2s . c o m return ~index; } }
From source file:Main.java
public static <T extends Comparable<T>> int addInOrder(final List<T> list, final T item) { final int insertAt; // The index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). final int index = Collections.binarySearch(list, item); if (index < 0) { insertAt = -(index + 1);//w w w.j a v a 2 s .co m } else { insertAt = index + 1; } list.add(insertAt, item); return insertAt; }
From source file:Main.java
public static <T extends Object> int binarySearch(final List<? extends Comparable<? super T>> list, final T element) { return Collections.binarySearch(list, element); }