java Collections.binarySearch 用法

package testCollections;

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

/**
* @Author: zhilei.wang
* @Date: 2019/11/11 14:08
* @Version 1.0
*/
public class TestCollection {
public static void main(String[] args) {
Student s1 = new Student("Alex", 10);
Student s2 = new Student("Jerry", 20);
Student s3 = new Student("Hustoy", 40);

List<Student> students = new ArrayList<>();
students.add(s1);
students.add(s2);
students.add(s3);

// Collections.sort(students);

// int searchResult = Collections.binarySearch(students, new Student("Jerry", 20));
int alex = Collections.binarySearch(students, new Student("Jerry", 20), new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//int num = this.age - o.age;
int num = o1.getAge() - o2.getAge();
int num1 = (num == 0 ? o1.getUsername().compareTo(o2.getUsername()) : num);
return num1;
}
});

System.out.println(alex);

}
}


class Student /*implements Comparable<Student> */ {
private String username;
private int age;

public Student(String username, int age) {
this.username = username;
this.age = age;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

/*
@Override
public int compareTo(Student o) {
int num = this.age - o.age;
int num1 = (num == 0 ? this.username.compareTo(o.username) : num);
return num1;
}

*/


}

猜你喜欢

转载自www.cnblogs.com/leigepython/p/11834854.html