Binary Search a LinkedList in Java

Description

The following code shows how to binary Search a LinkedList.

Example


// w  w  w.ja  v  a 2 s  .  c o m
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {

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

    for (char n = 'A'; n <= 'Z'; n++)
      ll.add(n);

    Collections.shuffle(ll);

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


    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));
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet