博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Comparator(CollectionsArraysSort)
阅读量:5246 次
发布时间:2019-06-14

本文共 4217 字,大约阅读时间需要 14 分钟。

概述:

有些场景需要对 数据结构中的数据进行排序,java中提供了sort方法, 可以通过自定义的comparator,指定排序方式,以下为例子及实现

使用

例子1

: comparator 比较 list 中对象属性

package JavaContainer.comparator;//https://www.cnblogs.com/rossoneri/p/4549529.htmlimport org.junit.Test;import java.util.*;//根据姓名排序class ComparatorName implements Comparator
{ boolean is_Ascend; public ComparatorName(boolean b) { // TODO Auto-generated constructor stub is_Ascend = b; } @Override public int compare(PersonComparaBean o1, PersonComparaBean o2) { //o1 o2 分别对应容器中前一个对象和后一个对象 if (is_Ascend) return o1.getName().compareTo(o2.getName()); else return o2.getName().compareTo(o1.getName()); }}//根据id排序class ComparatorId implements Comparator
{ private boolean aescOrDesc; public ComparatorId(boolean aescOrDesc) { this.aescOrDesc = aescOrDesc; } @Override public int compare(PersonComparaBean o1 , PersonComparaBean o2) { int a, b; if (aescOrDesc) { a = o1.getId(); b = o2.getId(); } else { a = o2.getId(); b = o1.getId(); } if (a > b) return 1; else if (a == b) return 0; else return -1; }}public class ComparatorDemo1 { @Test public void sortByNameOrId(){ PersonComparaBean personComparaBean1 = new PersonComparaBean(); personComparaBean1.setId(1); personComparaBean1.setName("ZZZZ"); PersonComparaBean personComparaBean2 = new PersonComparaBean(); personComparaBean2.setId(5); personComparaBean2.setName("CCCC"); PersonComparaBean personComparaBean4 = new PersonComparaBean(); personComparaBean4.setId(2); personComparaBean4.setName("DDDD"); PersonComparaBean personComparaBean3 = new PersonComparaBean(); personComparaBean3.setId(8); personComparaBean3.setName("AAAA"); List
list = new ArrayList<>(); list.add(personComparaBean1); list.add(personComparaBean2); list.add(personComparaBean3); list.add(personComparaBean4); for (PersonComparaBean personComparaBean : list){ System.out.println("原: "+personComparaBean); } /* 原: PersonComparaBean{id='1', name='ZZZZ'} 原: PersonComparaBean{id='5', name='CCCC'} 原: PersonComparaBean{id='8', name='AAAA'} 原: PersonComparaBean{id='2', name='DDDD'}*/ Collections.sort(list,new ComparatorId(true)); for (PersonComparaBean personComparaBean : list){ System.out.println("id: "+personComparaBean); } /*id: PersonComparaBean{id='1', name='ZZZZ'} id: PersonComparaBean{id='2', name='DDDD'} id: PersonComparaBean{id='5', name='CCCC'} id: PersonComparaBean{id='8', name='AAAA'}*/ Collections.sort(list,new ComparatorName(true)); for (PersonComparaBean personComparaBean : list){ System.out.println("name: "+personComparaBean); } /*name: PersonComparaBean{id='8', name='AAAA'} name: PersonComparaBean{id='5', name='CCCC'} name: PersonComparaBean{id='2', name='DDDD'} name: PersonComparaBean{id='1', name='ZZZZ'}*/ }}

例子2

: 比较二位数组 comparator

package JavaContainer.comparator;import org.junit.Test;import java.util.Arrays;import java.util.Comparator;//二维数组第二位比较(如果想要先判断第一位,然后第二位,可以在比较器中再加判断条件对比)public class ComparatorDemo2 {    @Test    public void test() {        int[][] nums = {
{1, 2}, {2, 3}, {1, 1}}; for (int[] num : nums) { for (int num1 : num) { System.out.print(num1); } System.out.println(" "); } Arrays.sort(nums, new Comparator
() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); for (int[] num : nums) { for (int num1 : num) { System.out.print(num1); } System.out.println(" "); } /*12 23 11 11 12 23 */ }}

转载于:https://www.cnblogs.com/yaoxublog/p/10930402.html

你可能感兴趣的文章
c#基础学习(0806)之抽象类实现多态
查看>>
S5PV210根文件系统的制作(一)
查看>>
51NOD 1244 莫比乌斯函数之和
查看>>
[bzoj1923]外星千足虫[高斯消元]
查看>>
centos下同时启动多个tomcat
查看>>
slab分配器
查看>>
分析 PHP大马-php_mof SHELL
查看>>
TCP/IP
查看>>
[推荐] 协同滤波 —— Collaborative Filtering (CF)
查看>>
python中使用中文
查看>>
数据清洗
查看>>
Android 动态加载 (二) 态加载机制 案例二
查看>>
MVC5 + EF6 + Bootstrap3 (10) 数据查询页面
查看>>
Windows下的Eclipse启动出现:a java runtime environment(JRE) or java development kit(JDK) must be.......
查看>>
PLC 通讯
查看>>
【读书笔记】C#高级编程 第三章 对象和类型
查看>>
python之decode、encode及codecs模块
查看>>
使用 Apache Pig 处理数据6
查看>>
Hadoop集群内lzo的安装与配置
查看>>
CASS 7.1 和 AutoCAD 2006的安装使用
查看>>