利用Comparator排序
import java.util.Comparator;
class Studentxx {
private String nameString;
private int age;
public Studentxx(String nameString, int age) {
// super();
this.nameString = nameString;
this.age = age;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Studentxx)) {
return false;
}
Studentxx studentxx = (Studentxx) obj;
if (studentxx.nameString.equals(this.nameString)
&& studentxx.age == this.age) {
return true;
} else {
return false;
}
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Studentxx [nameString=" + nameString + ", age=" + age + "]";
}
}
class StudentComparator implements Comparator<Studentxx> {
public int compare(Studentxx s1, Studentxx s2) {
if (s1.equals(s2)) {
return 0;
} else if (s1.getAge() < s2.getAge()) {
return 1;
} else {
return -1;
}
}
}
public class ComparatorDemo {
public static void main(String[] args) {
Studentxx stu[] = { new Studentxx("von", 20), new Studentxx("lee", 23),
new Studentxx("wong", 29), new Studentxx("cong", 23),
new Studentxx("sun", 39), new Studentxx("chao", 24) };
java.util.Arrays.sort(stu, new StudentComparator());
for (int i = 0; i < stu.length; i++) {
System.out.println(stu[i]);
}
}
}
java.util.Arrays.sort(Studentxx[] a, Comparator<? super Studentxx> c):
Sorts the specified array of objects according to the order induced by the specified comparator. All elements in the array must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the array).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.
- Parameters:
- a the array to be sorted
- c the comparator to determine the order of the array. A null value indicates that the elements' natural ordering should be used.
- Throws:
- ClassCastException - if the array contains elements that are not mutually comparable using the specified comparator.
comparator和comparable两个接口都可以实现相投的排序功能,但是与comparable接口相比,comparator接口是一种补救的做法。
利用Comparator排序的更多相关文章
- POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)
(点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...
- Java之——利用Comparator接口对多个排序条件进行处理
转载自:http://blog.csdn.net/l1028386804/article/details/56513205 膜拜大神··· 一.需求 假设现在有个如此的需求:需要对一个这样的雇员列表进 ...
- 我的Java开发学习之旅------>Java利用Comparator接口对多个排序条件进行处理
一需求 二实现Comparator接口 三验证排序结果 验证第一条件首先按级别排序级别最高的排在前面 验证第二条如果级别相等那么按工资排序工资高的排在前面 验证第三条如果工资相当则按入职年数排序入职时 ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- Java利用反射排序
前言 Java为我们提供了几种排序得方法,比如Arrays和Collections类,但是前提是数组或者集合中的元素都必须实现Comparable接口,基本的数据类型都已经实现了Comparable接 ...
- [Java] 使用Comparator排序对象
package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- Java8:使用Lambda表达式增强版Comparator排序
学习路上的自我记录-------路好长,就问你慌不慌,大声港,不慌.----jstarseven. 实体类: package com.server.model; /** * Created by js ...
- Comparator 排序 ArrayList 实操练习
package ltb6w; import java.util.Scanner;import java.util.ArrayList;import java.util.Comparator;impor ...
- NYOJ 8 一种排序(comparator排序)
一种排序 时间限制: 3000 ms | 内存限制: 65535 KB 难度: 3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都 ...
随机推荐
- 后台找到repeater里面的div并添加客户端点击事件
public partial class Inv_SelectWorkservice : System.Web.UI.Page,IPostBackEventHandler{ } 通过OnItemCre ...
- Mina入门:Java NIO基础概念
JDK1.4引入了Java NIO API(Java New IO),Java NIO得到了广泛应用.NIO允许程序进行非阻塞IO操作.java.nio.* 包括以下NIO基本结构: Buffer - ...
- Net常用命名空间和类介绍
一.基础命名空间 l System.Collections 包含了一些与集合相关的类型,比如列表,队列,位数组,哈希表和字典等. l System.IO 包含了一些数据流类型并提供了文件和目录同步 ...
- js实现弹出窗口、页面变成灰色并不可操作的例子
function show() //显示隐藏层和弹出层 { var hideobj=document.getElementById("hidebg"); hidebg.style. ...
- WPF子界面向父界面传递带参数的委托
需求如下: 父界面上有几个按钮,需要在点击按钮的时候向父界面传值,以知道该控制哪个界面显示. 父界面:WIN1 子界面:WIN2 此处或者说是子控件向父控件传值 1.子界面定义委托 WIN2里写: p ...
- c++常用IDE
vs2013. CodeLite (c/c++/PHP) codeBlocks Qt Creator Xcode
- Qt QtableView使用
ui->setupUi(this); ui->mainToolBar->hide(); tableView = new QTableView(this); // 设置表头 QStan ...
- 如何使代码审查更高效【摘自InfoQ】
代码审查者在审查代码时有非常多的东西需要关注.一个团队需要明确对于自己的项目哪些点是重要的,并不断在审查中就这些点进行检查. 人工审查代码是十分昂贵的,因此尽可能地使用自动化方式进行审查,如:代码 ...
- 关于三星手机照相机调用适配问题Android
因为三星手机在照相的时候,会自动将自动转化为为横屏. 这里的话 就不讲具体怎么实现的,因为在网络上是有相关的代码. 主要的是在你拍完照返回的时候,先前的界面可能会重绘,所以会导致一些问题. 我暂时的办 ...
- 精读《javascript高级程序设计》笔记一——基本概念
语法 严格模式 启用严格模式,在脚本顶部或函数内部上方添加"use strict";语句. 数据类型 typeof typeof返回undifined,boolean,number ...