package ---;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors; /**
* Created by zhugenqi on 2018/9/18 0018.
*/
class ListToMap {
private static List<Apple> appleList = new ArrayList<>();//存放apple对象集合; public static void main(String[] args) throws Exception {
Apple apple1 = new Apple(1, "苹果1", new BigDecimal("3.25"), 10);
Apple apple12 = new Apple(1, "苹果2", new BigDecimal("1.35"), 20);
Apple apple13 = new Apple(2, "苹果13", new BigDecimal("1.35"), 20);
Apple apple2 = new Apple(2, "香蕉", new BigDecimal("2.89"), 30);
Apple apple3 = new Apple(3, "荔枝", new BigDecimal("9.99"), 40);
appleList.add(apple1);
appleList.add(apple12);
appleList.add(apple13);
appleList.add(apple2);
appleList.add(apple3);
toMap(appleList);
} private static void toMap(List<Apple> appleList) throws Exception {
//转化为map,并且对 key 去重
Map<Integer, Apple> AppleMap = appleList.stream()
.collect(Collectors
.toMap(Apple::getId, apple -> apple, (k1, k2) -> k1)
);
//根据分组
Map<Integer, List<Apple>> AppleMap02 = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); //筛选出name为香蕉的code
List<Apple> AppleMap03 = appleList.stream().filter(apple -> apple.getName().equals("香蕉")).collect(Collectors.toList()); //分组求和
BigDecimal TotalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal sum = appleList.stream().filter(apple -> apple.getName().equals("香蕉")).map(Apple::getMoney).reduce(BigDecimal.ZERO,BigDecimal::add); //算出name的种类数量,并转为set
Set<String> names = appleList.stream().map(Apple::getName).collect(Collectors.toSet());
System.out.println("主键去从");
System.out.println(AppleMap);
System.out.println("分组");
for (Map.Entry entry : AppleMap02.entrySet()) {
System.out.print(entry.getKey() + " ");
List list = (List) entry.getValue();
for (Object object : list) {
if (list.indexOf(object) > 0) {
System.out.print(" ");
}
System.out.println(object);
}
}
System.out.println("过滤");
System.out.println(AppleMap03); System.out.println("求和");
System.out.println("总价格 :" + TotalMoney); System.out.println("分组求和");
System.out.println("总数量 : " + sum); System.out.println("输出种类");
System.out.println("种类 : "+names);
}
}

List的分组,求和,过滤操作的更多相关文章

  1. oracel sql分组求和过滤条件

  2. LINQ简明教程:数据排序、分组、过滤

    LINQ可以对很多数据源进行查询操作,比如数据库.数组(array).链表(list).XML文件等.在本文中,我将从数组中提取数据,这些数据是10个最受欢迎的国家.有一个类叫Countries,有c ...

  3. Oracle分析函数及常用函数: over(),rank()over()作用及用法--分区(分组)求和& 不连续/连续排名

    (1)   函数:  over()的作用及用法:    -- 分区(分组)求和. sum() over( partition by column1 order by column2 )主要用来对某个字 ...

  4. 【SQL必知必会笔记(3)】SELECT语句的WHERE子句数据过滤操作

    上个笔记主要介绍了利用SELECT语句检索单个/多个/所有列,并利用DISTINCT关键字检索具有唯一性的值.利用LIMIT/OFFSET子句限制结果:以及利用ORDER BY子句排序检索出的数据,主 ...

  5. MySQL单表查询(分组-筛选-过滤-去重-排序)

    目录 一:单表查询 1.单表查询(前期准备) 2.插入记录(写入数据) 3.查询关键字 二:查询关键字之where 1.查询id大于等于3小于等于6的数据 2.查询薪资是20000或者18000或者1 ...

  6. Oracle函数over(),rank()over()作用及用法--分区(分组)求和& 不连续/连续排名

    (1)   函数:  over()的作用及用法:    -- 分区(分组)求和. RANK ( ) OVER ( [query_partition_clause] order_by_clause )D ...

  7. 如何对Backbone.Collection进行过滤操作

    首先我想说的是这篇文章的题目起的很怪,因为我不知道起个什么名字比较好.渲染列表是我们应用中最常见的操作了吧,在运用Backbone的应用中,我们一般会把列表作为一个Collcetion,然后指定一个V ...

  8. Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)

    原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...

  9. linq分组求和_实体类和datatable

    1.数据分组求合,分别用的实体类以及datatable来分组求合,还有分组求和之后的如何取值 //实体类版本 List<ProgramTimeModel> TotalAllList = G ...

随机推荐

  1. adb.exe 已停止工作 解决

    netstat -aon|findstr 5037tasklist /fi "PID eq 10388"TASKKILL /F /IM PPAdbServer.exe

  2. jeecg字典表-系统字典

    新建字典 录入字典信息 添加类型 录入完类型后效果 新建列表用户 保存 同步数据库 同步完之后,对应的数据库中会创建对应的表. 测试表功能 保存之后,数据库保存对应的字段 生成代码 刷新工程之后,生成 ...

  3. springboot打jar包正常无法访问页面

    网上看到太多说版本换成 1.4.2.RELEASE. 可以将程序打成war包发布, 1.启动类改为 @Overrideprotected SpringApplicationBuilder config ...

  4. Vue中Vuex的详解与使用(简洁易懂的入门小实例)

    怎么安装 Vuex 我就不介绍了,官网上有 就是 npm install xxx 之类的.(其实就是懒~~~哈哈) 那么现在就开始正文部分了 众所周知 Vuex 是什么呢?是用来干嘛的呢? Vuex ...

  5. Debugging Beyond Visual Studio – WinDbg

    Getting started with WinDbg: 1. Download the Debugging Tools for Windows from the Microsoft website ...

  6. [模板] k短路

    简介 Dijkstra最短路+A*搜索. 先逆向求所有点到终点的最短路 \(dis[i]\). 定义估价函数 \(f[i] = d[i] + dis[i]\) , 其中 \(d[i]\) 表示当前起点 ...

  7. [问题]Android listView item edittext 不能调用软键盘输入法

    android listview item edittext not  softkeyboard edittext可以获取焦点, 可以触发事件, 但是就是不能调用输入法, 不知道为什么? 难道不能在i ...

  8. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  9. Tomcat 日志文件分割

    新到公司, 拿到了前辈们留下的 程序 “病历书” , 上面记载了项目上的一些 经常会犯的毛病,  还有相应的解决方法. 其中有的是因为后台 代码逻辑上的一些原因 , N手代码通病了吧 (这个还是以后再 ...

  10. busybox编译

    sync.c:(.text.sync_main+0x7c): undefined reference to `syncfs'Coreutils—>sync选项去掉 nsenter.c:(.tex ...