zoukankan      html  css  js  c++  java
  • 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;
    }

    */


    }
  • 相关阅读:
    ios-pch文件的手动添加
    iOS远程消息推送自我整理版
    iOS远程消息推送
    苹果App store 2015最新审核标准公布(2015.3)
    App上线基本流程
    iOS中常用的正则表达式
    如何获取App当前版本号
    添加Appicon的方法
    键盘弹出
    iOS9适配中出现的一些常见问题
  • 原文地址:https://www.cnblogs.com/leigepython/p/11834854.html
Copyright © 2011-2022 走看看