Java Collections binarySearch()

binarySearch() 用于返回对象的在排序列表中的位置。

1 语法

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

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

2 参数

list:指定需要搜索的集合列表。

key:需要搜索的关键词。

c:用于排序的比较器。

3 返回值

方法 返回值
binarySearch(List<? extends Comparable<? super T>> list, T key) 以升序排序列表返回键的索引。
binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 在比较器定义的顺序的排序列表中返回键的索引。

4 Collections binarySearch()示例1

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.binarySearch的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //创建List
        ArrayList<String> arrlist = new ArrayList<String>();
        arrlist.add("A");
        arrlist.add("B");
        arrlist.add("C");
        arrlist.add("D");
        //搜索'D'
        int index = Collections.binarySearch(arrlist, "D");
        System.out.println("index 'D' is available at position: "+index);
    }
}

输出结果为:

index 'D' is available at position: 3

5 Collections binarySearch()示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.binarySearch的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //创建List
        ArrayList<Integer> arrlist = new ArrayList<Integer>();
        arrlist.add(10);
        arrlist.add(-20);
        arrlist.add(30);
        arrlist.add(-40);
        arrlist.add(50);
        //输出List
        System.out.println("Provided List are: "+arrlist);
        //搜素'-20'
        int index = Collections.binarySearch(arrlist, -20,  Collections.reverseOrder());
        System.out.println("Index '-20' is available at position: "+index);
    }
}

输出结果为:

Provided List are: [10, -20, 30, -40, 50]
Index '-20' is available at position: -4

6 Collections binarySearch()示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.binarySearch的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //创建List
        ArrayList<Integer> arrlist = new ArrayList<Integer>();
        arrlist.add(10);
        arrlist.add(-20);
        arrlist.add(30);
        //输出List
        System.out.println("Provided List are: "+arrlist);
        System.out.print("Enter the search key: ");
        Scanner sc = new Scanner (System.in);
        String key = sc.next();
        int index = Collections.binarySearch(arrlist, key,  Collections.reverseOrder());
        System.out.println("Search Key is available at position: "+index);
        sc.close();
    }
}

输出结果为:

Provided List are: [10, -20, 30]
Enter the search key: A
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at java.lang.String.compareTo(String.java:111)
	at java.util.Collections$ReverseComparator.compare(Collections.java:5117)
	at java.util.Collections$ReverseComparator.compare(Collections.java:5108)
	at java.util.Collections.indexedBinarySearch(Collections.java:332)
	at java.util.Collections.binarySearch(Collections.java:320)
	at com.yiidian.Demo.main(Demo.java:24)

7 Collections binarySearch()示例4

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.binarySearch的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //创建List
        ArrayList<Integer> arlist = new ArrayList<Integer>();
        arlist.add(5);
        arlist.add(4);
        arlist.add(2);
        arlist.add(1);
        arlist.add(3);
        Collections.sort(arlist);
        //搜索'4'
        int index = Collections.binarySearch(arlist, 4, null );
        System.out.println("4 is available at index: "+index);
    }
}

输出结果为:

4 is available at index: 3

8 Collections binarySearch()示例5

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Collections.binarySearch的例子
 */
import java.util.*;

public class Demo {

    public static void main(String[] args) {
        //Create a list
        List<Domain> list = new ArrayList<Domain>();
        list.add(new Domain(10, "www.yiidian.com"));
        list.add(new Domain(20, "www.baidu.com"));
        list.add(new Domain(30, "www.google.com"));
        Comparator<Domain> c = new Comparator<Domain>()
        {
            public int compare(Domain u1, Domain u2)
            {
                return u1.getId().compareTo(u2.getId());
            }
        };
        /* Searching a domain with key value 10. To search
        we create an object of domain with key 10. */
        int index = Collections.binarySearch(list, new Domain(10, null), c);
        System.out.println("Available at index: " + index);
        //Searching an item with key 5
        index = Collections.binarySearch(list, new Domain(5, null), c);
        System.out.println("Found at index: " +index);
    }
}
//定义Domain对象
class Domain
{
    private int id;
    private String url;
    //构造方法
    public Domain(int id, String url)
    {
        this.id = id;
        this.url = url;
    }
    public Integer getId()
    {
        return Integer.valueOf(id);
    }
}

输出结果为:

Available at index: 0
Found at index: -1

 

热门文章

优秀文章