博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5-Java常用工具类-集合排序
阅读量:6279 次
发布时间:2019-06-22

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

Java工具类中提供了两种方法来排序:

java.util.Collections.sort(java.util.List)

java.util.Collections.sort(java.util.List, java.util.Comparator)

package cn.mtianyan.sort;public class Cat implements Comparable{    private String name; //名字    private int month; //年龄    private String species;//品种    @Override    public String toString() {        return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getMonth() {        return month;    }    public void setMonth(int month) {        this.month = month;    }    public String getSpecies() {        return species;    }    public void setSpecies(String species) {        this.species = species;    }    public Cat(String name, int month, String species) {        this.name = name;        this.month = month;        this.species = species;    }    @Override    public int compareTo(Object o) {        return this.month-((Cat)o).month;    }}
package cn.mtianyan.sort;import java.util.ArrayList;import java.util.Collections;import java.util.List;public class CatTest{    public static void main(String[] args) {        List
list = new ArrayList<>(); // 定义宠物猫对象 Cat huahua = new Cat("花花", 12, "英国短毛猫"); Cat fanfan = new Cat("凡凡", 3, "中华田园猫"); Cat huahua02 = new Cat("花花二代", 2, "英国短毛猫"); list.add(huahua); list.add(fanfan); list.add(huahua02); System.out.println("排序前宠物猫信息"); for (Cat cat: list) { System.out.println(cat); } // 执行排序操作 Collections.sort(list); // Cat必须实现Comparable接口,并实现其方法 System.out.println("按年龄从大到小排序之后宠物猫信息"); for (Cat cat: list) { System.out.println(cat); } }}

使用Collection.sort方法来进行排序,被排序对象必须实现Comparable接口,并重写compareTo方法

@Override    public int compareTo(Object o) {        return this.month-((Cat)o).month;    }

将此对象与指定的对象进行比较以进行排序。 返回一个负整数,零或正整数,分别对应该对象小于,等于或大于指定对象。

比如猫的例子,第一个12与第二个3进行比较。12-3=9是大于的。

下图提示语有错误: 应该为从小到大。

img_8b7731593baa5cd5ccb4e708b14b3d6d.jpe

名字字符串排序

@Override    public int compareTo(Object o) {        return ((Cat)o).name.compareTo(this.name);    }
// 定义宠物猫对象        Cat huahua = new Cat("aa", 12, "英国短毛猫");        Cat fanfan = new Cat("bb", 3, "中华田园猫");        Cat huahua02 = new Cat("cc", 2, "英国短毛猫");

运行结果:

img_3e02906607ab74251252016a7ddd2754.jpe

Collections.sort(List list, Comparator<T> comparator)

package cn.mtianyan.sort;public class CatTwo {    private String name; //名字    private int month; //年龄    private String species;//品种    @Override    public String toString() {        return "[姓名:" + name + ", 年龄:" + month + ", 品种:" + species + "]";    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getMonth() {        return month;    }    public void setMonth(int month) {        this.month = month;    }    public String getSpecies() {        return species;    }    public void setSpecies(String species) {        this.species = species;    }    public CatTwo(String name, int month, String species) {        this.name = name;        this.month = month;        this.species = species;    }}
package cn.mtianyan.sort;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class CatTwoTest {    public static void main(String[] args) {        List
list = new ArrayList<>(); // 定义宠物猫对象 CatTwo huahua = new CatTwo("aa", 12, "英国短毛猫"); CatTwo fanfan = new CatTwo("bb", 3, "中华田园猫"); CatTwo huahua02 = new CatTwo("cc", 2, "英国短毛猫"); list.add(huahua); list.add(fanfan); list.add(huahua02); System.out.println("排序前宠物猫信息"); for (CatTwo cat: list) { System.out.println(cat); } Comparator
comparator = new Comparator
() { @Override public int compare(CatTwo o1, CatTwo o2) { return o1.getMonth() - o2.getMonth(); } }; Collections.sort(list,comparator); System.out.println("排序后宠物猫信息(按月份从小到大)"); for (CatTwo cat: list) { System.out.println(cat); } }}

运行结果:

img_c978b90d74bd8ebc476ee381a0f9df00.jpe

字符串名字从大到小

@Override            public int compare(CatTwo o1, CatTwo o2) {//                return o1.getMonth() - o2.getMonth();                return o2.getName().compareTo(o1.getName());            }        };        Collections.sort(list,comparator);        System.out.println("排序后宠物猫信息(按名字从大到小)");

运行结果:

img_d7e39af04065f06960469092af0d916a.jpe
package cn.mtianyan.sort;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;public class SortArray {    public static void main(String[] args) {        Character [] charArray = new Character[]{'m','t','i','a','n','y','a','n'};        List
list = Arrays.asList(charArray); System.out.println("====Character排序前===="); for (Character character:list) { System.out.print(character+" "); } System.out.println(); System.out.println("====Character正序排序后===="); Collections.sort(list); for (Character character:list) { System.out.print(character+" "); } System.out.println(); System.out.println("====Character逆序排序后===="); Collections.sort(list,Collections.reverseOrder()); for (Character character:list) { System.out.print(character+" "); } String [] stringArray = new String[]{"yes","ha","mtianyan","ab"}; System.out.println(); List
list1 = Arrays.asList(stringArray); System.out.println("====String排序前===="); for (String string:list1) { System.out.print(string+" "); } System.out.println(); Collections.sort(list1); System.out.println("====String正序排序后===="); for (String string:list1) { System.out.print(string+" "); } Comparator cmp1 = Collections.reverseOrder(); Collections.sort(list1,cmp1); System.out.println(); System.out.println("====String逆序排序后===="); for (String string:list1) { System.out.print(string+" "); } Integer[] intArray = new Integer[]{8,6,3,4,7,5,1}; List
list2 = Arrays.asList(intArray); System.out.println(); System.out.println("====Integer排序前===="); for (Integer integer:list2) { System.out.print(integer+" "); } System.out.println(); System.out.println("====Integer正序排序后===="); Collections.sort(list2); for (Integer integer:list2) { System.out.print(integer+" "); } System.out.println(); System.out.println("====Integer逆序排序后===="); Collections.sort(list2,Collections.reverseOrder()); for (Integer integer:list2) { System.out.print(integer+" "); } }}

运行结果:

img_e3d2578f52a4537243d99be5db0a8b56.jpe
package cn.mtianyan.sort;import java.util.HashSet;import java.util.Set;import java.util.concurrent.ConcurrentSkipListSet;public class SortedMap {    public static void main(String[] args) {        Set
set = new HashSet<>(); set.add("ccc"); set.add("ddd"); set.add("eee"); set.add("aaa"); set.add("bbb"); for (String string:set){ System.out.println(string); } System.out.println("============="); ConcurrentSkipListSet
concurrentSkipListSet = new ConcurrentSkipListSet<>(); concurrentSkipListSet.add("ccc"); concurrentSkipListSet.add("ddd"); concurrentSkipListSet.add("eee"); concurrentSkipListSet.add("aaa"); concurrentSkipListSet.add("bbb"); for (String string:concurrentSkipListSet) { System.out.println(string); } }}

运行结果:

img_14770957351635592d3da5243e152cb3.jpe

SortedMap接口的实现类ConcurrentSkipListMap

下一节我们将学习泛型。

转载地址:http://pofva.baihongyu.com/

你可能感兴趣的文章
ArcGIS Engine 符号自动化配置工具实现
查看>>
小程序 · 跳转带参数写法,兼容url的出错
查看>>
flutter error
查看>>
Flask框架从入门到精通之模型数据库配置(十一)
查看>>
10年重新出发
查看>>
2019年-年终总结
查看>>
聊聊elasticsearch的RoutingService
查看>>
让人抓头的Java并发(一) 轻松认识多线程
查看>>
从源码剖析useState的执行过程
查看>>
地包天如何矫正?
查看>>
中间件
查看>>
Android SharedPreferences
查看>>
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>