博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分组工具类
阅读量:7272 次
发布时间:2019-06-29

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

hot3.png

/**   * @Description: 分组工具类   *   * @Author: hepengfei   * @Date: 15:43 2018/3/20   */public class GroupUtils {     /**      * eg:      * List list = devider(list, (Apple o1, Apple o2) -> {      *      return o1.getWeight() == o2.getWeight() ?0 : 1;      * });      * @param datas 带分组的list      * @param c      * @param 
* @return */ public static
List
> devider(Collection
datas, Comparator
c){ List
> result = new ArrayList
>(); for (T t : datas) { boolean isSame = false; for (int i=0;i
innerList = new ArrayList
(); result.add(innerList); innerList.add(t); } } return result; } /** demo */ public void test(List list){ List devider = devider(list, (Apple o1, Apple o2) -> { return o1.getWeight() == o2.getWeight() ?0 : 1; }); System.out.println(devider); } class Apple{ private int weight; public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } }}

 

2.Comparable,Comparator

Comparable是为了对某个类的集合进行排序,所以此时一般都是这个需要排序的类本身去实现Comparable接口;倘若我们要根据其他规则排序,可以匿名内部类或者lambda表达式使用Comparator接口:        list.sort(Comparator.comparing(Employee::getSalary).thenComparing(Employee::getName));

转载于:https://my.oschina.net/JackieRiver/blog/1647883

你可能感兴趣的文章
load average
查看>>
浏览器标签栏logo添加
查看>>
setjmp()/longjmp()的使用方法
查看>>
SQL Server 2008R2的各种问题及解决方案
查看>>
为什么字符串正则里有好多的反斜杠
查看>>
A.Eugeny and Array
查看>>
rzchecktree实现单选以及隐藏选择框
查看>>
amazon 面经3
查看>>
hibernate主键详细介绍
查看>>
【整理】uclibc,eglibc,glibc之间的区别和联系
查看>>
Python Scrapy 爬虫(四):部署与运行
查看>>
bat 每天开机自动从git/svn服务器更新代码
查看>>
Poj 3669 Meteor Shower
查看>>
深度学习【二】机器学习的通用流程
查看>>
具有参考意义的博客园地址
查看>>
网站一些常见问题
查看>>
linux安装总结(亲测)
查看>>
隐藏控制台
查看>>
bootstrap之增删改查
查看>>
EWS Managed API 2.0 设置获取邮件自动回复功能
查看>>