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

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

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

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

其实也是依据Comarator这个类

  1. 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:英文翻译即可
  1. int compare(T o1, T o2)
  2. Compares its two arguments for order.
  3. static <T,U extends Comparable<? super U>>
  4. Comparator<T> comparing(Function<? super T,? extends U> keyExtractor)
  5. Accepts a function that extracts a Comparable sort key from a type T, and returns a Comparator<T> that compares by that sort key.
  6. static <T,U> Comparator<T> comparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
  7. 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.
  8. static <T> Comparator<T> comparingDouble(ToDoubleFunction<? super T> keyExtractor)
  9. Accepts a function that extracts a double sort key from a type T, and returns a Comparator<T> that compares by that sort key.
  10. static <T> Comparator<T> comparingInt(ToIntFunction<? super T> keyExtractor)
  11. Accepts a function that extracts an int sort key from a type T, and returns a Comparator<T> that compares by that sort key.
  12. static <T> Comparator<T> comparingLong(ToLongFunction<? super T> keyExtractor)
  13. Accepts a function that extracts a long sort key from a type T, and returns a Comparator<T> that compares by that sort key.
  14. boolean equals(Object obj)
  15. Indicates whether some other object is "equal to" this comparator.
  16. static <T extends Comparable<? super T>>
  17. Comparator<T> naturalOrder()
  18. Returns a comparator that compares Comparable objects in natural order.
  19. static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
  20. Returns a null-friendly comparator that considers null to be less than non-null.
  21. static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
  22. Returns a null-friendly comparator that considers null to be greater than non-null.
  23. default Comparator<T> reversed()
  24. Returns a comparator that imposes the reverse ordering of this comparator.
  25. static <T extends Comparable<? super T>>
  26. Comparator<T> reverseOrder()
  27. Returns a comparator that imposes the reverse of the natural ordering.
  28. default Comparator<T> thenComparing(Comparator<? super T> other)
  29. Returns a lexicographic-order comparator with another comparator.
  30. default <U extends Comparable<? super U>>
  31. Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor)
  32. Returns a lexicographic-order comparator with a function that extracts a Comparable sort key.
  33. default <U> Comparator<T> thenComparing(Function<? super T,? extends U> keyExtractor, Comparator<? super U> keyComparator)
  34. Returns a lexicographic-order comparator with a function that extracts a key to be compared with the given Comparator.
  35. default Comparator<T> thenComparingDouble(ToDoubleFunction<? super T> keyExtractor)
  36. Returns a lexicographic-order comparator with a function that extracts a double sort key.
  37. default Comparator<T> thenComparingInt(ToIntFunction<? super T> keyExtractor)
  38. Returns a lexicographic-order comparator with a function that extracts a int sort key.
  39. default Comparator<T> thenComparingLong(ToLongFunction<? super T> keyExtractor)
  40. Returns a lexicographic-order comparator with a function that extracts a long sort key.

代码:

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

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

代码:

  1. Collections.sort(rateInfolist, new Comparator<RateInfo>(){
  2. /*
  3. * int compare(RateInfo R1, RateInfo R2) 返回一个基本类型的整型,
  4. * 返回负数表示:R1 小于R2,
  5. * 返回0 表示:R1和R2相等,
  6. * 返回正数表示:R1大于R2
  7. */
  8. public int compare(RateInfo R1, RateInfo R2) {
  9. Integer rateCode1 = Integer.parseInt(R1.getRateCode());
  10. Integer rateCode2 = Integer.parseInt(R2.getRateCode());
  11. //按照RateCode的年龄进行升序排列
  12. if(rateCode1 > rateCode2){
  13. return 1;
  14. }
  15. if(rateCode1 == rateCode2){
  16. return 0;
  17. }
  18. return -1;
  19. }
  20. });

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

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. 3列滚动抽奖 jquery.slotmachine

    效果图: 需引入js文件: <script src="js/jquery-3.2.0.js"></script> <script src=" ...

  2. ajax无刷新上传

    我们在使用上传控件的时候,会遇到刷新的问题,最近使用ajax做的上传,觉得效果还是很不错. 首先,我们需要在页面上放上上传控件:需要注意的是,我们必须放在form里面,实现表单上传.  <for ...

  3. CAS机制详解

    目录 1. 定义 2. 实现原理 3. 无版本号CAS实战说明 4. CAS机制在Java中的应用 5. CAS的缺点 1. CPU开销过大 2. 不能保证代码块的原子性 3. ABA问题 6. JA ...

  4. 服务器访问数据库表mysql

    服务器的MySQL配置就不说了,直接说一些用到的基础命令 登陆 show databases; use 数据库: show tables; 执行sql即可: 一定要有分号 select * from ...

  5. 【笔记篇】C#笔记1

    返回目录:目录请戳这里~ 以后的C#笔记如果不出意外的话都是Win10 Professional + VS2015 Professional出的,(当然还有直接在编译框敲的所以能不能过编译我也不知道┑ ...

  6. 利用HttpWebRequest模拟提交图片

    利用HttpWebRequest模拟提交图片 最近在做排量post工具, 以前做的都是提交文字 这次需要post图片过去,弄了半天终于弄好了: /// <summary> /// Post ...

  7. IDEA Error:java: Compilation failed: internal java compiler error 解决方案

    这是由于版本不一致导致的 file => settings => 搜索找到Java Compiler 把相应jdk版本改成1.8 ctrl+alt+s

  8. webpack 清理旧打包资源插件

    当我们修改带hash的文件并进行打包时,每打包一次就会生成一个新的文件,而旧的文件并 没有删除.为了解决这种情况,我们可以使用clean-webpack-plugin 在打包之前将文件先清除,之后再打 ...

  9. IOS6 新特性之UIActivityViewController详解

    新的IOS6增加了一些新特性.因为应用需要,所以在国庆的几天里.研究了一下IOS6的说明文档,然后大概地总结了一下UIActivityViewController的用法与大家分享. 首先 从实际效果入 ...

  10. 转-Pycharm中运行Python代码的几种方式

    转自:Pycharm中运行Python代码的几种方式 在pycharm中的Python代码运行会出现各种奇葩的问题,比如,密码输入时不显示或没有提示,给我们带来一些麻烦,下面介绍几种代码运行的几种方式 ...