Collections在对自定义对象进行排序时,自定义类需要对compareTo()函数进行重写。


public class Student implements Comparable<Student>{//实现Comparable接口,范型定义为类自己 private String name;
private Integer age; @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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;
} public Student(String name, Integer age) {
this.name = name;
this.age = age;
} public Student() { } @Override
public int compareTo(Student o) { return this.getAge() - o.getAge();//按age升序
//return o.getAge() - this.getAge();//按age降序
//return 0;// 0表示所有数据都相等
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; public class Main { public static void main(String[] args) { Student good = new Student("good", 19);
Student bad = new Student("bad", 15);
Student normal = new Student("normal", 21); List<Student> list = new ArrayList<>(); Collections.addAll(list,good,bad,normal);
System.out.println(list); Collections.sort(list);
System.out.println(list); } }

如果用sort带排序参数的方法,则自定义类可以不用写compareTo()方法。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List; public class Main { public static void main(String[] args) { Student good = new Student("good", 19);
Student bad = new Student("bad", 15);
Student normal = new Student("normal", 21); List<Student> list = new ArrayList<>(); Collections.addAll(list,good,bad,normal);
System.out.println(list); Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) { return o1.getAge() - o2.getAge();//按age升序
return o2.getAge() - o1.getAge();//按ag降序
//return 0;
}
}); System.out.println(list);
} }

JAVA基础补漏--Collections工具类排序的更多相关文章

  1. 【BigData】Java基础_定义工具类,对ArrayList排序并且求最大值、最小值、平均值

    需求描述 编写一个工具类,对ArrayList实现以下功能: ① 排序 ② 求最大值 ③ 求最小值 ④ 求平均值 需求实现 实现代码 package cn.test.logan.day04; impo ...

  2. Java容器---Arrays & Collections工具类

    1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...

  3. JAVA Collections工具类sort()排序方法

    主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...

  4. java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合

    Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...

  5. TreeMap和TreeSet在排序时如何比较元素?Collections工具类中的sort()方法如何比较元素?

    TreeSet要求存放的对象所属的类必须实现Comparable接口,该接口提供了比较元素的compareTo()方法,当插入元素时会回调该方法比较元素的大小.TreeMap要求存放的键值对映射的键必 ...

  6. Java:集合,Collections工具类用法

    Collections工具类提供了大量针对Collection/Map的操作,总体可分为四类,都为静态(static)方法: 1. 排序操作(主要针对List接口相关) reverse(List li ...

  7. Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)

    集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...

  8. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  9. 用斗地主的实例学会使用java Collections工具类

    目录 一.背景 二.概念 1.定义 2.方法 2.1.排序方法 2.2.查找/替换方法 三.斗地主实例 3.1.代码结构 3.2.常量定义 3.3.单只牌类 3.4.玩家类 3.5.主程序 四.深入理 ...

随机推荐

  1. LeetCode—Longest Consecutive Sequence

    题目描述: Given an unsorted array of integers, find the length of the longest consecutive elements seque ...

  2. react setstate

    1.prevstate参数 https://segmentfault.com/q/1010000008177874 2.不是实时渲染 http://bbs.reactnative.cn/topic/3 ...

  3. 荣誉墙项目day28 django常用函数

    1.在网页上渲染字符串from django.http import HttpResponsereturn HttpResponse(u"hello world") 2.渲染网页f ...

  4. HttpRunner 参数化数据驱动

    HttpRunner 2.0 参数化数据驱动案例,废话不说,直接上干货. 1.测试用例目录结构      api:接口集 testcases:测试用例    testsuites:测试套件 data: ...

  5. SIP中的 session, dialog 及 transaction 的解释

    http://stackoverflow.com/questions/35133331/difference-between-session-dialog-and-transaction-in-sip ...

  6. sipp模拟电信运营商VoIP终端测试(SIP协议调试)

    三大运营商都有SIP服务器,用来支持语音对讲,多媒体调度等功能,他们的平台可能不是标准的SIP协议会话. 为了应对没完没了的对接各个厂商的平台,这里再整理了一套协议脚本,毕竟全都是没有意义的无用功,标 ...

  7. 专项训练错题整理-nowcoder-算法

    一.排序 1.快速排序在下列哪种情况下最易发挥其长处? 答案是: 被排序的数据完全无序. 在数据基本有序的情况下,会退化为冒泡排序,复杂度会退化为O(n^2). ①[因为,如果是基本有序的话, 那么每 ...

  8. flask自定义session

    根据内置session原理可以进行session的定制: #!/usr/bin/env python # -*- coding:utf-8 -*- import uuid import json fr ...

  9. zen-cart安装出现时区错误解决办法

    有时候在安装zen-cart的时候出现时区错误,提示: ERROR: date.timezone not set in php.ini. Please contact your hosting com ...

  10. 一步一步学EF系列四【升级篇 实体与数据库的映射】

    之前的三张为基础篇,如果不考虑架构问题,做一般的小程序,以足够用了.基本的增删改查也都有了.但是作为学习显然是不够的.通过之前三章的学习,有没有发现这样写有什么问题,有没有觉得繁琐的?可能有人会说,之 ...