用Comparator排序和分组
Test实体
import java.util.Objects; /**
* @author gallen
* @description
* @date 2018/11/16
* @time 18:57
*/
public class Test { Integer id; String name; Integer age; Integer sex; public Test(Integer id, String name, Integer age, Integer sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
} public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return this.age;
} public void setAge(Integer age) {
this.age = age;
} public Integer getSex() {
return this.sex;
} public void setSex(Integer sex) {
this.sex = sex;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Test)) return false;
Test test = (Test) o;
return Objects.equals(this.getId(), test.getId()) &&
Objects.equals(this.getName(), test.getName()) &&
Objects.equals(this.getAge(), test.getAge()) &&
Objects.equals(this.getSex(), test.getSex());
} @Override
public int hashCode() {
return Objects.hash(this.getId(), this.getName(), this.getAge(), this.getSex());
} @Override
public String toString() {
return "Test{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
排序、分组
import java.util.Objects; /**
* @author gallen
* @description
* @date 2018/11/16
* @time 18:57
*/
public class Demo { public static void main(String[] args) {
List<Test> list = Lists.newArrayList();
list.add(new Test(1, "zhangsan", 37, 1));
list.add(new Test(2, "lisi", 35, 2));
list.add(new Test(3, "wangwu", 25, 2));
list.add(new Test(4, "zhaoliu", 67, 1));
//排序
Collections.sort(list, new Comparator<Test>() {
@Override
public int compare(Test o1, Test o2) {
return o1.id.compareTo(o2.id);
}
});
list1.stream().forEach(test -> {
System.out.println("list1 = " + test.toString());
});
//分组
List<List<Test>> list2 = divider(list, new Comparator<Test>() {
@Override
public int compare(Test o1, Test o2) {
if (o1.age.intValue() > 30 && o2.age.intValue() > 30) {
return 0;
} else if (o1.age.intValue() > 30 && o2.age.intValue() <= 30) {
return 1;
} else if (o1.age.intValue() <= 30 && o2.age.intValue() > 30) {
return -1;
} else if (o1.age.intValue() <= 30 && o2.age.intValue() <= 30) {
return 0;
}else{
return 0;
}
}
});
list2.stream().forEach(test -> {
System.out.println("list2 = " + test.toString());
});
}
}
用Comparator排序和分组的更多相关文章
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- LINQ简明教程:数据排序、分组、过滤
LINQ可以对很多数据源进行查询操作,比如数据库.数组(array).链表(list).XML文件等.在本文中,我将从数组中提取数据,这些数据是10个最受欢迎的国家.有一个类叫Countries,有c ...
- MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)
MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作): a.创建2张表 create table userinfo(nid int not nul ...
- MySql下实现先排序后分组
最近在工作中遇到一个先排序后分组的需求,发现MySql不同的版本有不同的结果,特此记录. 举例:要求在shop表中查询出各类型商店中价格最高的商品. --表结构-- create table `sho ...
- LINQ之路14:LINQ Operators之排序和分组(Ordering and Grouping)
本篇继续LINQ Operators的介绍,这里要讨论的是LINQ中的排序和分组功能.LINQ的排序操作符有:OrderBy, OrderByDescending, ThenBy, 和ThenByDe ...
- ElasticSearch6.0 Java API 使用 排序,分组 ,创建索引,添加索引数据,打分等(一)
ElasticSearch6.0 Java API 使用 排序,分组 ,创建索引,添加索引数据,打分等 如果此文章对你有帮助,请关注一下哦 1.1 搭建maven 工程 创建web工程 ...
- mysql 怎样先排序再分组
权游游牧族:众所周知!一句SqL语句不能先排序再分组.所以这里给出几个案例 --表结构-- create table `shop` ( `id` int (10) PRIMARY KEY, `shop ...
- sql中实现先排序后分组
数据表结构和数据如下: CREATE TABLE `commun_message_chat_single` ( `id` ) NOT NULL AUTO_INCREMENT, `chat_id` ) ...
- MySQL进阶5--分组函数 / 分组排序和分组查询 group by(having) /order by
MySQL进阶--分组排序和分组查询 group by(having) /order by /* 介绍分组函数 功能:用做统计使用,又称为聚合函数或组函数 1.分类: sum, avg 求和 /平均数 ...
随机推荐
- SQL2000,2005,2008安装在一台机子上
工欲善其事,必先利其器.本机的系统是在网上自己下载的,是32位windows7旗舰版.因为学习,需要在一台机子上同时安装SQL Server2000,2005,2008三个版本的数据库.先是在网上查了 ...
- @PropertySource
功能 加载指定的属性文件(*.properties)到 Spring 的 Environment 中.可以配合 @Value 和 @ConfigurationProperties 使用. @Prope ...
- 架构演进历程及为什么选择Spring Cloud
单体式架构: 垂直拆分: 垂直拆分的特点: 分布式服务: 分布式服务的特点: SOA面向服务的架构: 服务治理: 微服务: 微服务结构: 服务调用方式: http客户端工具:
- 开机报错 the connected AC adapter has a lower wattage than the recommended model which was shipped with the system。
机型:联想Thinkpad T410 报错场景:在电脑插上电源充电情况下开机,会自动进入bios setup utility提示你需要重新设置日期时间.date/time 报错提示:The conne ...
- 洛谷 P1807 最长路_NOI导刊2010提高(07)
最长路 #include <iostream> #include <cstdio> #include <cstring> #include <queue> ...
- eclipse3.4+对的处理插件(附SVN插件安装实例)
Eclipse 3.4以前安装插件无非有两种方式, 直接copy插件到features/plugins目录或者在links目录下创建链接文件. Eclipse 3.4又推出另一种新的安装途径, 更加灵 ...
- (二)我的JavaScript系列:JavaScript面向对象旅程(下)
剪不断,理还乱,是离愁. 前面已经提到过新语言开发的两个步骤,分别是:一.定义基本的数据类型,完善结构化编程语言的设计:二.为函数类型绑定this的概念,好在对象的方法中可以引用到对象自身.下面是继续 ...
- 看动画,秒懂人工智能&物联网
- 洛谷 P2383 狗哥玩木棒
题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第一行是一个整数n表示测试 ...
- Unity3D中使用Projector生成阴影
在Unity3D中使用Projector实现动态阴影 无意中看见一篇博客叙述使用Projector实现动态阴影可以在移动平台拥有非常好的性能,遂按照其想法实现了一遍,发现其中竟有许多细节,写下这篇博客 ...