方法一:根据java1.8lambda表达式进行排序

Comparator<RateInfo> comparator = (t1, t2) -> t1.getRateCode().compareTo(t2.getRateCode());

方法二:使用List的方法sort()排序

List API:default void sort(Comparator<? super E> c)

其实也是依据Comarator这个类

rateInfolist.sort(comparator.reversed());

方法三:使用Collections类的sort进行排序

static <T> void sort(List<T> list, Comparator<? super T> c)

Sorts the specified list according to the order induced by the specified comparator.
谷歌翻译:根据指定比较器引发的顺序对指定列表进行排序。
其实该排序也是使用Comparator类进行排序
Comparator类API:英文翻译即可
int     compare(T o1, T o2)
Compares its two arguments for order.
static <T,U extends Comparable<? super U>>
Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
Accepts a function that extracts a sort key from a type T, and returns a Comparator<T> that compares by that sort key using the specified Comparator.
static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
Accepts a function that extracts a double sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
Accepts a function that extracts an int sort key from a type T, and returns a Comparator<T> that compares by that sort key.
static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
Accepts a function that extracts a long sort key from a type T, and returns a Comparator<T> that compares by that sort key.
boolean equals(Object obj)
Indicates whether some other object is "equal to" this comparator.
static <T extends Comparable<? super T>>
Comparator<T> naturalOrder()
Returns a comparator that compares Comparable objects in natural order.
static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers null to be less than non-null.
static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
Returns a null-friendly comparator that considers null to be greater than non-null.
default Comparator<T> reversed()
Returns a comparator that imposes the reverse ordering of this comparator.
static <T extends Comparable<? super T>>
Comparator<T> reverseOrder()
Returns a comparator that imposes the reverse of the natural ordering.
default Comparator<T> thenComparing(Comparator<? super T> other)
Returns a lexicographic-order comparator with another comparator.
default <U extends Comparable<? super U>>
Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a Comparable sort key.
default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator.
default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a double sort key.
default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a int sort key.
default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
Returns a lexicographic-order comparator with a function that extracts a long sort key.

代码:

Collections.sort(rateInfolist, Comparator.comparing(RateInfo::getRateCode));

方法四:使用Comparator的匿名对象类重写compare方法

代码:

Collections.sort(rateInfolist, new Comparator<RateInfo>(){
/*
* int compare(RateInfo R1, RateInfo R2) 返回一个基本类型的整型,
* 返回负数表示:R1 小于R2,
* 返回0 表示:R1和R2相等,
* 返回正数表示:R1大于R2
*/
public int compare(RateInfo R1, RateInfo R2) {
Integer rateCode1 = Integer.parseInt(R1.getRateCode());
Integer rateCode2 = Integer.parseInt(R2.getRateCode());
//按照RateCode的年龄进行升序排列
if(rateCode1 > rateCode2){
return 1;
}
if(rateCode1 == rateCode2){
return 0;
}
return -1;
}
});

自己写代码时遇到的问题,根据我的理解和网上的资料做的总结

java 集合存储对象且根据对象属性排序的更多相关文章

  1. 如何对List集合中的对象进行按某个属性排序

    我们在实际的开发工作中,经常会碰到排序的问题,如题,我们如何针对List集合中的某一个属性进行排序 当list集合中的元素类型是我们自定义类型时,有两种对list中的元素进行排序的方法: 方法一 让l ...

  2. LinkedList中将对象按照某一属性排序

    例如,链表 treelist 声明如下: LinkedList<TreeNode> treelist = new LinkedList<TreeNode>(); 其中 Tree ...

  3. Java 集合存储都返回什么?

    1.抛出一个类 package com.math.spring; import com.google.common.collect.Lists; import com.google.common.co ...

  4. Java基础知识强化之集合框架笔记06:Collection集合存储自定义对象并遍历的案例

    1.练习:用集合存储5个学生对象,并把学生对象进行遍历. 分析: (1)创建学生类(2)创建集合对象(3)创建学生对象(4)把学生添加到集合(5)把集合转成数组(6)遍历数组 2. 代码示例: Stu ...

  5. js 对象数组根据对象中的属性排序

    function createComparisonFunction(propertyName){ return function(object1,object2){ var value1 = obje ...

  6. java集合简介

    java集合主要包括以下几点 Java 集合概述 Collection 接口 Iterator 接口 Set List Map Collections 工具类 Enumeration 1.java集合 ...

  7. Java集合知识总结

    集合概述 集合:集合是Java中提供的一种容器,可以用来存储多个数据. 集合和数组的区别: (1)数组长度的是固定的,集合的长度是可变的. (2)数组中存储的都是同一类型的元素.集合存储的都是对象,对 ...

  8. Java集合入门

    内容: 1.认识集合 2.Iterator迭代器 1.认识集合 (1)什么是集合 前面的学习,我们知道数据多了,使用数组存放.而且数组中存放的都是基本类型的数据,并且数组是定长的. 当在程序中创建的对 ...

  9. Java集合----概述、Collection接口、Iterator接口

    Java 集合概述 Java 集合就像一种容器,可以把多个对象的引用放入容器中. Java 集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组 Java 集合可分为 Set.Li ...

随机推荐

  1. 深入理解Android-清晰的理解Service

    1.什么是Service 2.Service的生命周期 3.Service的工作过程 4.Service的start和bind状态有什么区别? 5.同一个Service,先startService,然 ...

  2. js字符实体 转义字符串

    HTML字符实体(Character Entities),转义字符串(Escape Sequence) 为什么要用转义字符串? HTML中<,>,&等有特殊含义(<,> ...

  3. Java内存问题:java.lang.OutOfMemoryError: PermGen space

    代码运行环境: jdk1.7.0_25 apache-tomcat-8.0.30 详细报错日志: org.springframework.web.util.NestedServletException ...

  4. db.Database.SqlQuery完成分页封装

    调用代码: string sql = @"SELECT a.Id ,c.Title,a.Content,a.Status,b.ReportSum FROM dbo.Comment AS a ...

  5. go string和[ ]byte

    https://www.cnblogs.com/zhangboyu/p/7623712.html

  6. Python全栈开发:html标签

    Html是什么? htyper text markup language 即超文本标记语言 超文本: 就是指页面内可以包含图片.链接,甚至音乐.程序等非文字元素. 标记语言: 标记(标签)构成的语言. ...

  7. druapl-note1 本地开发上传模块不提示Ftp的警告

    刚安装完drupal之后,通过drupalxray 看到其它drupal网站安装的一些模块,下载好模块并安装时,提示需要输入Ftp信息. 但是本地开发不输入Ftp信息的(也不清楚自己的系统是否开启Ft ...

  8. LoadRunner内部结构(1)

    LoadRunner内部结构(1) 根据http://www.wilsonmar.com/1loadrun.htm  翻译: LoadRunner内部结构 1,            被测系统是由驱动 ...

  9. BCB怎么调用DLL中的函数

    推荐你看<BCB编写DLL终极手册>这篇文章如下片段:二. 静态调用 DLL使用 $BCB path\Bin\implib.exe 生成 Lib 文件,加入到工程文件中将该文件拷贝到当前目 ...

  10. centos6 php7 安装 memcache 和 memcached

    下载安装memcache 注意:官网的memcache包,暂时好像不支持php7.所以到下面地址下载memcache包,切换到php7分支 php7 memcache github 下载地址 官网下载 ...