Java Collections.binarySearch() 方法



描述

Java binarySearch(List<? extends Comparable<? super T>>, T) 方法用於使用二分查詢演算法在指定的列表中搜索指定的物件。

宣告

以下是 java.util.Collections.binarySearch() 方法的宣告。

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

引數

  • list − 這是要搜尋的列表。

  • key − 這是要搜尋的鍵。

返回值

如果該方法呼叫在列表中包含搜尋鍵,則返回搜尋鍵的索引。

異常

ClassCastException − 如果列表包含並非相互可比較的元素,則丟擲此異常。

Java Collections.binarySearch(List<? extends T> list,T key,Comparator<? super T> c) 方法

描述

binarySearch(List<? extends T>, T, Comparator<? super T>) 方法用於使用二分查詢演算法在指定的列表中搜索指定的物件。該列表必須根據指定的比較器按升序排序。

宣告

以下是 java.util.Collections.binarySearch() 方法的宣告。

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

引數

  • list − 這是要搜尋的列表。

  • key − 這是要搜尋的鍵。

  • c − 這是列表排序所依據的比較器。空值表示應使用元素的自然排序。

返回值

如果該方法呼叫在列表中包含搜尋鍵,則返回搜尋鍵的索引。

異常

ClassCastException − 如果列表包含使用指定的比較器無法相互比較的元素,則丟擲此異常。

在字串集合上執行二分查詢示例

以下示例演示了 Java Collection binarySearch(Collection,T) 方法的使用,用於在指定的集合中搜索專案。我們建立了一個包含一些字串的 List 物件,列印了原始列表。使用 binarySearch(Collection, T) 方法,我們從列表中搜索了一個鍵,然後列印了搜尋結果。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("Welcome","to","Tutorialspoint"));

      System.out.println("Collection: " + list);
      // search the list for item 'To'
      int index = Collections.binarySearch(list, "to");     

      System.out.println("'To' is available at index: "+index);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Collection: [Welcome, to, Tutorialspoint]
'To' is available at index: 1

在可比較物件的集合上執行二分查詢示例

以下示例演示了 Java Collection binarySearch(Collection,T) 方法的使用,用於在指定的集合中搜索專案。我們建立了一個包含一些 Student 物件的 List 物件,列印了原始列表。使用 binarySearch(Collection, T) 方法,我們從列表中搜索了一個學生,然後列印了搜尋結果。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Collection: " + list);
      // search the list for student 'Julie'
      int index = Collections.binarySearch(list, new Student(1, "Julie"));     

      System.out.println("Julie is available at index: "+index);
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Collection: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Julie is available at index: 0

使用比較器在物件集合上執行二分查詢示例

以下示例演示了 Java Collection binarySearch(Collection,T, Comparator) 方法的使用,用於在指定的集合中搜索專案。我們建立了一個包含一些 Student 物件的 List 物件,列印了原始列表。使用 binarySearch(Collection, T, Comparator) 方法,我們從列表中搜索了一個學生,然後列印了搜尋結果。

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Comparator;

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Collection: " + list);
      RollNoComparator comparator = new RollNoComparator();
      // search the list for student 'Julie'
      int index = Collections.binarySearch(list, new Student(1, "Julie"),comparator);     

      System.out.println("Julie is available at index: "+index);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   public int getRollNo() {
      return rollNo;
   }

   public void setRollNo(int rollNo) {
      this.rollNo = rollNo;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

class RollNoComparator implements Comparator<Student>{

   @Override
   public int compare(Student o1, Student o2) {
      return o1.getRollNo()-o2.getRollNo();
   }	
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Collection: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Julie is available at index: 0
java_util_collections.htm
廣告