binarySearch() 是 Java Collections 类的内置方法,它返回对象在排序列表中的位置。有两种不同类型的 Java 集合 binarySearch() 方法,可以根据其参数进行区分。
这些是:
- Java Collections binarySearch(List<? extends Comparable<? super T>> list, T key)
- Java Collections binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Java Collections binarySearch(List<? extends Comparable<? super T>> list, T key)
此方法用于使用二进制搜索算法搜索指定对象的提供列表。在进行方法调用之前,必须根据指定的自然数对列表进行升序排序。如果列表未排序,则结果未定义。
Java Collections binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
此方法用于使用二进制搜索算法搜索指定对象的提供列表。在进行方法调用之前,必须根据指定的比较器将列表按升序排序。
用法
以下是 binarySearch() 方法的声明:
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)
参数
参数 | 描述 | 必需/可选 |
---|---|---|
list | 由用户指定要搜索。 | Required |
key | 它是要在提供的列表中搜索的键。 | Required |
c | 它是用于对列表进行排序的比较器。 | Required |
返回
方法 | 返回 |
---|---|
binarySearch(List<? extends Comparable<? super T>> list, T key) | 它在升序的排序列表中返回键的索引。 |
binarySearch(List<? extends T> list, T key, Comparator<? super T> c) | 它返回由比较器 c 定义的排序列表中的键的索引。 |
异常
ClassCastException - 如果列表的元素不能相互比较或者搜索关键字与列表的元素不能相互比较,则抛出异常。
兼容版本
Java 1.5 及以上
例子1
import java.util.*;
public class CollectionsBinarySearchExample1 {
public static void main(String[] args) {
//Create list
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
//Search the list for key 'D'
int index = Collections.binarySearch(arrlist, "D");
System.out.println("index 'D' is available at position:"+index);
}
}
输出:
index 'D' is available at position:3
例子2
import java.util.*;
public class CollectionsBinarySearchExample2 {
public static void main(String[] args) {
//Create list
ArrayList<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(10);
arrlist.add(-20);
arrlist.add(30);
arrlist.add(-40);
arrlist.add(50);
//Print List
System.out.println("Provided List are:"+arrlist);
//Search the list for key '-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
例子3
import java.util.*;
public class CollectionsBinarySearchExample3 {
public static void main(String[] args) {
//Create list
ArrayList<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(10);
arrlist.add(-20);
arrlist.add(30);
//Print 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:D Exception in thread "main" java.lang.ClassCastException:java.base/java.lang.Integer cannot be cast to java.base/java.lang.String at java.base/java.lang.String.compareTo(String.java:124) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5140) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5131) at java.base/java.util.Collections.indexedBinarySearch(Collections.java:333) at java.base/java.util.Collections.binarySearch(Collections.java:321) at myPackage.CollectionBinarySearchExample3.main(CollectionBinarySearchExample3.java:16)
示例 4
import java.util.*;
public class CollectionsBinarySearchExample4 {
public static void main(String[] args) {
//Create 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);
//Search for key
int index = Collections.binarySearch(arlist, 4, null );
System.out.println("4 is available at index:"+index);
}
}
输出:
4 is available at index:3
例 5
import java.util.*;
public class CollectionsBinarySearchExample5 {
public static void main(String[] args) {
//Create a list
List<Domain> list = new ArrayList<Domain>();
list.add(new Domain(10, "www.JavaTpoint.com"));
list.add(new Domain(20, "www.sssit.com"));
list.add(new Domain(30, "www.hindi100.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);
}
}
//user-defined class to store domains with id and url
class Domain
{
private int id;
private String url;
// Constructor
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
相关用法
- Java Collections binarySearch()用法及代码示例
- Java Collections synchronizedSortedSet()用法及代码示例
- Java Collections checkedQueue()用法及代码示例
- Java Collections unmodifiableNavigableSet()用法及代码示例
- Java Collections checkedSet()用法及代码示例
- Java Collections copy()用法及代码示例
- Java Collections checkedMap()用法及代码示例
- Java Collections synchronizedNavigableSet()用法及代码示例
- Java Collections singleton()用法及代码示例
- Java Collections fill()用法及代码示例
- Java Collections nCopies()用法及代码示例
- Java Collections emptySet()用法及代码示例
- Java Collections newSetFromMap()用法及代码示例
- Java Collections checkedSortedMap()用法及代码示例
- Java Collections addAll()用法及代码示例
- Java Collections sort()用法及代码示例
- Java Collections emptySortedSet()用法及代码示例
- Java Collections max()用法及代码示例
- Java Collections checkedSortedSet()用法及代码示例
- Java Collections checkedCollection()用法及代码示例
注:本文由纯净天空筛选整理自 Java Collections binarySearch() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。