java.util.Collections.binarySearch()方法是一個java.util.Collections類方法,該方法返回對象在排序列表中的位置。
// Returns index of key in sorted list sorted in // ascending order public static int binarySearch(List slist, T key) // Returns index of key in sorted list sorted in // order defined by Comparator c. public static int binarySearch(List slist, T key, Comparator c) If key is not present, the it returns "(-(insertion point) - 1)". The insertion point is defined as the point at which the key would be inserted into the list.
如果list的元素與指定的比較器不具有可比性,或者該元素的搜索關鍵字不可替代,則該方法將引發ClassCastException。
在按升序排序的列表中搜索int鍵:
// Java program to demonstrate working of Collections.
// binarySearch()
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class GFG
{
public static void main(String[] args)
{
List al = new ArrayList();
al.add(1);
al.add(2);
al.add(3);
al.add(10);
al.add(20);
// 10 is present at index 3.
int index = Collections.binarySearch(al, 10);
System.out.println(index);
// 13 is not present. 13 would have been inserted
// at position 4. So the function returns (-4-1)
// which is -5.
index = Collections.binarySearch(al, 13);
System.out.println(index);
}
}
輸出:
3 -5
在以降序排列的列表中搜索int鍵。
// Java program to demonstrate working of Collections.
// binarySearch() in an array sorted in descending order.
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class GFG
{
public static void main(String[] args)
{
List al = new ArrayList();
al.add(100);
al.add(50);
al.add(30);
al.add(10);
al.add(2);
// The last parameter specifies the comparator method
// used for sorting.
int index = Collections.binarySearch(al, 50,
Collections.reverseOrder());
System.out.println("Found at index " + index);
}
}
輸出:
Found at index 1
搜索用戶定義的類對象的列表:
// Java program to demonstrate working of Collections.
// binarySearch() in a list of user defined objects
import java.util.*;
class Binarysearch
{
public static void main(String[] args)
{
// Create a list
List<Domain> l = new ArrayList<Domain>();
l.add(new Domain(10, "quiz.geeksforgeeks.org"));
l.add(new Domain(20, "practice.geeksforgeeks.org"));
l.add(new Domain(30, "code.geeksforgeeks.org"));
l.add(new Domain(40, "www.geeksforgeeks.org"));
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(l,
new Domain(10, null), c);
System.out.println("Found at index " + index);
// Searching an item with key 5
index = Collections.binarySearch(l,
new Domain(5, null), c);
System.out.println(index);
}
}
// A 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);
}
}
輸出:
0 -1
數組.binarysearch() vs Collections.binarySearch()
Arrays.binarysearch()適用於也可以是原始數據類型的數組。 Collections.binarysearch()適用於ArrayList和LinkedList之類的對象集合。
重要事項:
- 如果輸入列表未排序,則結果不確定。
- 如果有重複項,則不能保證將找到哪一個。
- 對於ArrayList之類的“random access”列表,此方法以log(n)時間運行。如果指定的列表未實現RandomAccess接口且很大,則此方法將執行基於迭代器的二進製搜索,該搜索執行O(n)鏈接遍曆和O(log n)元素比較。
相關用法
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java.util.concurrent.RecursiveAction用法及代碼示例
- Java Java.util.concurrent.Phaser用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
注:本文由純淨天空篩選整理自 Collections.binarySearch() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。