使用比较器使用二进制搜索从列表中搜索用户定义对象的Java程序
Java中的 Comparator 接口可用于比较用户定义的对象。 Comparator 接口存在于Java.util 包中。二分搜索是一种搜索算法,它使用分而治之的规则来搜索列表或数组中元素的存在。二分搜索算法仅适用于排序列表。如果列表未排序,搜索可能会返回未定义的结果。实现 Collection 接口的预定义类也实现了可比较的接口。因此,当对预定义包装类的对象执行二分搜索时,对象按自然顺序排序,然后对排序列表执行二分搜索。默认情况下,用户定义的类不实现Java中的Comparable接口。对于用户定义的类,Comparator 接口非常有用。 Comparator 接口允许比较用户定义类的特定属性。 Comparator 接口还可用于比较属于不同类的两个不同对象。实现 Comparator 接口的类会覆盖接口的compare()方法。以下示例处理创建用户定义的对象并使用 Comparator 接口对对象进行排序,最后执行二分查找以获取所需对象的索引。
句法
public static int binarySearch(List list, T key)
参数:
- list:要对其执行二进制搜索的对象列表。
- key:要查找的对象
返回值:返回关键元素的索引。如果未找到关键元素,则返回的索引为 (-(insertion point) – 1)。
示例 1:
创建一个学生班级,其属性为学生 ID、学生姓名和学生分数。 Student 类对象列表没有排序。使用传递给 Collections.sort() 方法的比较器对象对学生对象列表进行排序。使用 Collections,binarySearch() 方法对已排序的 Student 列表执行二分搜索。如果找到目标对象,则返回相应的索引,否则返回的索引为 -(插入点)- 1。插入点是目标元素如果不存在则必须放置在列表中的索引。每个学生的学生 ID 都是唯一的,因此它用于对学生列表进行排序。 compare() 方法在StudentComp类中被覆盖。如果两个学生的学生 ID 相同,compare() 方法返回 0,如果 s1 的学生 ID 大于 s2,则返回 +1,否则返回 -1。学生列表按升序排列。最后,对已排序的对象调用二分查找,并将索引所需的对象打印到控制台。
代码实现
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student {
// attributes
int sid;
String name;
int marks;
// constructor
public Student(int sid, String name, int marks)
{
super();
this.sid = sid;
this.name = name;
this.marks = marks;
}
// returns student id
public int getSid() { return sid; }
// returns student name
public String getName() { return name; }
// returns marks
public int getMarks() { return marks; }
public void setSid(int sid) { this.sid = sid; }
public void setName(String name) { this.name = name; }
public void setMarks(int marks) { this.marks = marks; }
}
// Comparator to sort a list
class StudentComp implements Comparator {
@Override public int compare(Student s1, Student s2)
{
if (s1.getSid() == s2.getSid()) {
return 0;
}
else if (s1.getSid() > s2.getSid()) {
return 1;
}
else if (s1.getSid() < s2.getSid()) {
return -1;
}
return -1;
}
}
public class BinarySearchDemo {
public static void main(String[] args)
{
// list of students
ArrayList l = new ArrayList();
// Add students
l.add(new Student(100, "Jack", 95));
l.add(new Student(101, "Jane", 98));
l.add(new Student(199, "Mary", 90));
l.add(new Student(105, "Beck", 75));
l.add(new Student(104, "Betty", 85));
l.add(new Student(103, "Archie", 96));
l.add(new Student(108, "Nate", 89));
l.add(new Student(109, "Liam", 100));
// sort the list
Collections.sort(l, new StudentComp());
Student searchKey = new Student(109, "Liam", 100);
// index of the target
int index1 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index1);
searchKey = new Student(99, "Jennifer", 60);
int index2 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index2);
}
}
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student {
// attributes
int sid;
String name;
int marks;
// constructor
public Student(int sid, String name, int marks)
{
super();
this.sid = sid;
this.name = name;
this.marks = marks;
}
// returns student id
public int getSid() { return sid; }
// returns name
public String getName() { return name; }
// returns marks
public int getMarks() { return marks; }
public void setSid(int sid) { this.sid = sid; }
public void setName(String name) { this.name = name; }
public void setMarks(int marks) { this.marks = marks; }
}
// comparator to sort
class StudentComp implements Comparator {
@Override public int compare(Student s1, Student s2)
{
if (s1.getMarks() == s2.getMarks()) {
return 0;
}
else if (s1.getMarks() < s2.getMarks()) {
return 1;
}
return -1;
}
}
public class BinarySearchDemo {
public static void main(String[] args)
{
// list of students
ArrayList l = new ArrayList();
// Add students
l.add(new Student(100, "Jack", 95));
l.add(new Student(101, "Jane", 98));
l.add(new Student(199, "Mary", 90));
l.add(new Student(105, "Beck", 75));
l.add(new Student(104, "Betty", 85));
l.add(new Student(103, "Archie", 96));
l.add(new Student(108, "Nate", 89));
l.add(new Student(109, "Liam", 100));
// sort the list
Collections.sort(l, new StudentComp());
for (int i = 0; i < l.size(); i++)
System.out.println(i + " " + l.get(i).getName()
+ " " + l.get(i).getMarks());
// search the target
Student searchKey = new Student(109, "Liam", 100);
int index1 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index1);
searchKey = new Student(99, "Jennifer", 60);
int index2 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index2);
}
}
Index of the searched key: 6
Index of the searched key: -1
示例 2:
在此示例中,Student 列表按 Student 类的标记属性的降序排序。因此,上一个示例的相同对象现在具有不同的索引。对新列表执行二分搜索,因此返回所需元素的索引并打印到输出。
代码实现
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Student {
// attributes
int sid;
String name;
int marks;
// constructor
public Student(int sid, String name, int marks)
{
super();
this.sid = sid;
this.name = name;
this.marks = marks;
}
// returns student id
public int getSid() { return sid; }
// returns name
public String getName() { return name; }
// returns marks
public int getMarks() { return marks; }
public void setSid(int sid) { this.sid = sid; }
public void setName(String name) { this.name = name; }
public void setMarks(int marks) { this.marks = marks; }
}
// comparator to sort
class StudentComp implements Comparator {
@Override public int compare(Student s1, Student s2)
{
if (s1.getMarks() == s2.getMarks()) {
return 0;
}
else if (s1.getMarks() < s2.getMarks()) {
return 1;
}
return -1;
}
}
public class BinarySearchDemo {
public static void main(String[] args)
{
// list of students
ArrayList l = new ArrayList();
// Add students
l.add(new Student(100, "Jack", 95));
l.add(new Student(101, "Jane", 98));
l.add(new Student(199, "Mary", 90));
l.add(new Student(105, "Beck", 75));
l.add(new Student(104, "Betty", 85));
l.add(new Student(103, "Archie", 96));
l.add(new Student(108, "Nate", 89));
l.add(new Student(109, "Liam", 100));
// sort the list
Collections.sort(l, new StudentComp());
for (int i = 0; i < l.size(); i++)
System.out.println(i + " " + l.get(i).getName()
+ " " + l.get(i).getMarks());
// search the target
Student searchKey = new Student(109, "Liam", 100);
int index1 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index1);
searchKey = new Student(99, "Jennifer", 60);
int index2 = Collections.binarySearch(
l, searchKey, new StudentComp());
System.out.println("Index of the searched key: "
+ index2);
}
}
0 Liam 100
1 Jane 98
2 Archie 96
3 Jack 95
4 Mary 90
5 Nate 89
6 Betty 85
7 Beck 75
Index of the searched key: 0
Index of the searched key: -9