Example usage for java.util Collections binarySearch

List of usage examples for java.util Collections binarySearch

Introduction

In this page you can find the example usage for java.util Collections binarySearch.

Prototype

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) 

Source Link

Document

Searches the specified list for the specified object using the binary search algorithm.

Usage

From source file:Main.java

public static void main(String[] argv) {

    List<String> sortedList = new LinkedList<String>();
    sortedList.addAll(Arrays.asList(new String[] { "a", "b", "c", "d" }));

    int index = Collections.binarySearch(sortedList, "c");
    System.out.println(index);//from www.j  a  va2s.  co  m
    index = Collections.binarySearch(sortedList, "e");
    System.out.println(index);
}

From source file:Main.java

public static void main(String[] argv) {

    List<String> sortedList = new LinkedList<String>();
    sortedList.addAll(Arrays.asList(new String[] { "a", "b", "c", "d" }));

    int index = Collections.binarySearch(sortedList, "c");
    System.out.println(index);/*from   w  ww  .  ja va  2 s.co m*/

    index = Collections.binarySearch(sortedList, "e");
    System.out.println(index);
}

From source file:Main.java

public static void main(String args[]) {

    List<String> arlst = new ArrayList<String>();

    arlst.add("CSS");
    arlst.add("HTML");
    arlst.add("PHP");

    // search the list for key 
    int index = Collections.binarySearch(arlst, "HTML");

    System.out.println(index);//from www . ja  va2 s . c om
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List<String> sortedList = new LinkedList<String>();
    sortedList.addAll(Arrays.asList(new String[] { "a", "b", "c", "d" }));

    int index = Collections.binarySearch(sortedList, "c");

    index = Collections.binarySearch(sortedList, "cc");
}

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();

    v.add("Z");//from w ww.  java2s  .  c o  m
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");

    Collections.sort(v);
    System.out.println(v);

    int index = Collections.binarySearch(v, "D");
    System.out.println("Element found at : " + index);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Java");
    list.add("R");
    list.add("CSS");
    list.add("XML");

    Collections.sort(list);//from  w w  w  .  j  a  va  2s.co  m
    System.out.println("List: " + list);

    int index = Collections.binarySearch(list, "CSS");
    System.out.println("CSS in List  is at " + index);

    index = Collections.binarySearch(list, "Javascript");
    System.out.println("Javascript in List is  at " + index);

}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("1");
    arrayList.add("4");
    arrayList.add("2");
    arrayList.add("5");
    arrayList.add("3");

    Collections.sort(arrayList);//from   w  w  w.j a  va  2  s.c o m
    System.out.println("Sorted ArrayList contains : " + arrayList);
    int index = Collections.binarySearch(arrayList, "4");
    System.out.println("Element found at : " + index);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new LinkedList<String>();

    list.add("a");
    list.add("b");
    list.add("c");
    list.add("d");
    list.add("e");
    list.add("f");

    Collections.sort(list);//from   w w  w. j  av a2s. c  o  m
    System.out.println(list);
    int index = Collections.binarySearch(list, "c");
    if (index > 0) {
        System.out.println("Found at index = " + index);
        String month = (String) list.get(index);
        System.out.println(month);
    }
}

From source file:SearchTest.java

public static void main(String args[]) {
    String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };

    // Convert to list
    ArrayList list = new ArrayList(Arrays.asList(simpsons));

    // Ensure list sorted
    Collections.sort(list);//  w  w w  .  j  a v a 2s.  co 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);
}

From source file:MainClass.java

public static void main(String args[]) {
    String str[] = { "B", "H", "L", "M", "I", "N", "R" };
    // Convert to list
    List list = new ArrayList(Arrays.asList(str));
    // Ensure list sorted
    Collections.sort(list);/*www.j a va2  s.  com*/
    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);
    // Insert
    int newIndex = -index - 1;
    list.add(newIndex, "J");
    System.out.println("With J added: [length: " + list.size() + "]");
    System.out.println(list);
}