Java中Collections的binarySearch方法
方法一
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
此方法传入一个实现了Comparable接口的对象类的列表和要查找的元素。
创建实现了Comparable接口的对象类
public class Student1 implements Comparable<Student1> {
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int compareTo(Student1 s) {
int num = this.age - s.age;
int num1 = (num == 0 ? this.name.compareTo(s.name) : num);
return num1;
}
}
调用
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import cn.stone.comparable_comparator.Student1; public class Student1Test {
public static void main(String[] args) {
List<Student1> list1 = new ArrayList<Student1>();
list1.add(new Student1("林青霞", 27));
list1.add(new Student1("风清扬", 30));
list1.add(new Student1("刘晓曲", 28));
list1.add(new Student1("武鑫", 29));
list1.add(new Student1("林青霞", 27)); int index=Collections.binarySearch(list1, new Student1("林青霞", 27));
System.out.println(index);
}
}
方法二
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
此方法传入一个列表,要查找的元素,以及一个比较器。
创建对象类
public class Student2 {
private String name;
private int age;
public Student2() {
}
public Student2(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
调用
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import cn.stone.comparable_comparator.Student2; public class Student2Test {
public static void main(String[] args) {
List<Student2> list2 = new ArrayList<Student2>();
list2.add(new Student2("林青霞", 27));
list2.add(new Student2("风清扬", 30));
list2.add(new Student2("刘晓曲", 28));
list2.add(new Student2("武鑫", 29));
list2.add(new Student2("林青霞", 27)); int index1 = Collections.binarySearch(list2, new Student2("林青霞", 27),
new MyComparator());
System.out.println(index1);
}
} class MyComparator implements Comparator<Student2> {
@Override
public int compare(Student2 s1, Student2 s2) {
int num = s1.getAge() - s2.getAge();
int num1 = (num == 0 ? s1.getName().compareTo(s2.getName()) : num);
return num1;
}
}
注1:排序必须是升序
注2:方法二比较器也可采用匿名类实现
Java中Collections的binarySearch方法的更多相关文章
- Java中Collections的frequency方法
注:调用此方法时需要根据自己的须由复写Objects的equals方法 创建复写了equals方法的对象类 public class Student { private String name; pr ...
- java中substring的使用方法
java中substring的使用方法 str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str: str ...
- Java中Set的contains()方法
Java中Set的contains()方法 -- hashCode与equals方法的约定及重写原则 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashCode() a ...
- [java,2017-05-16] java中清空StringBuffer的方法以及耗费时间比较
java中清空StringBuffer的方法,我能想到的有4种: 1. buffer.setLength(0); 设置长度为0 2. buffer.delete(0, buffer.length() ...
- java中BorderLayout的使用方法
相关设置: 使用BorderLayout布局上下左右中布局5个按键,单击中间的那个按键时就关闭窗口 代码: /**** *java中BorderLayout的使用方法 * 使用BorderLayout ...
- 【Java】Java中常用的String方法
本文转载于:java中常用的String方法 1 length()字符串的长度 String a = "Hello Word!"; System.out.println(a.len ...
- Java中Set的contains()方法——hashCode与equals方法的约定及重写原则
转自:http://blog.csdn.net/renfufei/article/details/14163329 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashC ...
- java中equals和hashCode方法随笔二
前几天看了篇关于java中equals和hashCode方法的解析 1.Object类中的equals方法和hashCode方法. Object类中的equals和hashCode方法简单明了,所有的 ...
- java中static变量和方法的总结
转自:http://blog.csdn.net/haobo920/article/details/5921621 java中static变量和方法的总结 java中一切皆是对象 一个类中对象的定义一般 ...
随机推荐
- JAVA EE配TOMCAT
纯粹就是吧百度教程上的过程走了一遍发现不行综合各种教程配出来了,四张图代表了四个阶段,以后再要配的话直接来这里看.
- 20145202 《Java程序设计》第四周学习总结
继承:打破了封装性 extends 1.提高了代码的复用性. 2.让类与类之间产生了关系,有了这个关系,才有了多态的特性. 3.必须是类与类之间有所属类关系才可以继承. 4.java只支持单继承不支持 ...
- Struts2---配置文件讲解及简单登录示例
bean 用于创建一个JavaBean实例 constant 用于Struts2默认行为标签 <!-- 配置web默认编码集,相当于HttpServletRequest.setChartacte ...
- CodeForces 873F Forbidden Indices 后缀数组
忘了当时怎么做的了,先把代码贴上,保存一下后缀数组模板. #include <cstdio> #include <cstring> #include <algorithm ...
- Android启动屏全屏显示
1.为首页面设置一个theme <style name="app_start" > <item name="android:windowNoTitle& ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目11
2014-03-21 20:20 题目:给定一个只包含‘0’.‘1’.‘|’.‘&’.‘^’的布尔表达式,和一个期望的结果(0或者1).如果允许你用自由地给这个表达式加括号来控制运算的顺序,问 ...
- python之列表/元组/字典/字符串
一.列表 格式:list = ['xxx','xxx','xxx'] 性质:可以修改列表内容 copy用法: import copy names = ['] names01 = names #直接引用 ...
- CSU-2110 Keeping Cool
题目链接 http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2110 题目 Description Kevin has just got ...
- 【Python】重定向 Stream 到文件
Python 系统模块 sys 中有三个变量 stdin . stdout 与 stderr ,分别对应标准输入流.输出流与错误流.stdin 默认指向键盘, stdout 与 stderr 默认指向 ...
- Anaconda基本使用
anaconda常用使用命令 显示安装程序包列表 conda list 选择其它的源 conda config --add channels https://mirrors.tuna.tsinghua ...