1、分组,计数,排序

public class Java8Example1 {

    public static void main(String[] args) {

        List<String> items = Arrays.asList(
"apple", "apple",
"orange", "orange", "orange",
"blueberry",
"peach", "peach", "peach", "peach"
); // 分组,计数
Map<String, Long> result = items.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); System.out.println(result); Map<String, Long> finalMap = new LinkedHashMap<>();
// 排序
result.entrySet().stream()
.sorted(Map.Entry.<String, Long>comparingByValue().reversed())
.forEachOrdered(e -> finalMap.put(e.getKey(), e.getValue())); System.out.println(finalMap);
}
}

2、以下例子使用的类

public class Item {

    private String name;

    private int qty;

    private BigDecimal price;

    public Item() {
} public Item(String name, int qty, BigDecimal price) {
this.name = name;
this.qty = qty;
this.price = price;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getQty() {
return qty;
} public void setQty(int qty) {
this.qty = qty;
} public BigDecimal getPrice() {
return price;
} public void setPrice(BigDecimal price) {
this.price = price;
} @Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", qty=" + qty +
", price=" + price +
'}';
}
}

3、分组,计数,计算数量

public class Java8Example2 {

    public static void main(String[] args) {

        List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal(23.5)),
new Item("apple", 20, new BigDecimal(32.5)),
new Item("orange", 30, new BigDecimal(13.9)),
new Item("orange", 20, new BigDecimal(33.5)),
new Item("orange", 10, new BigDecimal(63.5)),
new Item("orange", 50, new BigDecimal(41.5)),
new Item("peach", 20, new BigDecimal(26.5)),
new Item("peach", 30, new BigDecimal(42.5)),
new Item("peach", 40, new BigDecimal(24.5)),
new Item("peach", 10, new BigDecimal(12.5))
); // 分组,计数
Map<String, Long> counting = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.counting()));
System.out.println(counting); // 分组,计数,数量
Map<String, Integer> sum = items.stream()
.collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));
System.out.println(sum); }
}

4、通过价格分组

public class Java8Example3 {

    public static void main(String[] args) {

        List<Item> items = Arrays.asList(
new Item("apple", 10, new BigDecimal(23.5)),
new Item("apple", 20, new BigDecimal(32.5)),
new Item("orange", 30, new BigDecimal(13.9)),
new Item("orange", 20, new BigDecimal(32.5)),
new Item("orange", 10, new BigDecimal(63.5)),
new Item("orange", 50, new BigDecimal(41.5)),
new Item("peach", 20, new BigDecimal(26.5)),
new Item("peach", 30, new BigDecimal(32.5)),
new Item("peach", 40, new BigDecimal(24.5)),
new Item("peach", 10, new BigDecimal(12.5))
); // 分组
Map<BigDecimal, List<Item>> groupByPriceMap = items.stream()
.collect(Collectors.groupingBy(Item::getPrice));
System.out.println(groupByPriceMap); // 分组 转化List->Set
Map<BigDecimal, Set<String>> result = items.stream()
.collect(Collectors.groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));
System.out.println(result);
}
}

Java8分组(groupingBy)的更多相关文章

  1. java8 先groupingBy 后map

    Map<Integer,List<String>> mapBanJI_UserNameList=list.stream().collect(Collectors.groupin ...

  2. Java8 stream 中利用 groupingBy 进行多字段分组求和

    Java8的groupingBy实现集合的分组,类似Mysql的group by分组功能,注意得到的是一个map 对集合按照单个属性分组 case1: List<String> items ...

  3. 乐字节-Java8新特性-接口默认方法之Stream流(下)

    接上一篇:<Java8新特性之stream>,下面继续接着讲Stream 5.流的中间操作 常见的流的中间操作,归为以下三大类:筛选和切片流操作.元素映射操作.元素排序操作: 操作 描述 ...

  4. 乐字节-Java8核心特性实战之Stream(流)

    说起流,我们会想起手机 ,电脑组装流水线,物流仓库商品包装流水线等等.如果把手机 ,电脑,包裹看做最终结果的话,那么加工商品前的各种零部件就可以看做数据源,而中间一系列的加工作业操作,就可以看做流的处 ...

  5. 简洁又快速地处理集合——Java8 Stream(下)

    上一篇文章我讲解 Stream 流的基本原理,以及它与集合的区别关系,讲了那么多抽象的,本篇文章我们开始实战,讲解流的各个方法以及各种操作 没有看过上篇文章的可以先点击进去学习一下 简洁又快速地处理集 ...

  6. Java8初体验(二)Stream语法详解---符合人的思维模式,数据源--》stream-->干什么事(具体怎么做,就交给Stream)--》聚合

    Function.identity()是什么? // 将Stream转换成容器或Map Stream<String> stream = Stream.of("I", & ...

  7. java8(2)--- Stream API

    1.简述 Java8中有两大最为重要的改变.第一个是 Lambda 表达式:另外一 个就是 Stream API. Stream 是处理集合的抽象概念,它可以指定你希望对集合进行的操作,可以执行非常复 ...

  8. Java8 新特性之集合操作Stream

    Java8 新特性之集合操作Stream Stream简介 Java 8引入了全新的Stream API.这里的Stream和I/O流不同,它更像具有Iterable的集合类,但行为和集合类又有所不同 ...

  9. JAVA8学习——从使用角度深入Stream流(学习过程)

    Stream 流 初识Stream流 简单认识一下Stream:Stream类中的官方介绍: /** * A sequence of elements supporting sequential an ...

随机推荐

  1. Java中“==”和“equals()”的区别

    “==”比较的是变量所指向的对象,当S1在内存中定义以后,再定义s2的时候s2所指向的值是定义s1时候所创建的,而不是又在内存创建了一个“Monday”. “equals”比较的是对象所指向的值,所以 ...

  2. mysql中各种日期数据类型及其所占用的空间

    DATETIME,8字节: DATE,3字节: TIMESTAMP,4字节: YEAR,1字节: TIME,3字节:

  3. mysql技巧

    SELECT id,is_deleted,position,1/position as p,status FROM application.view_entry order by p desc;.// ...

  4. java script基本数据类型与数组

    基本数据类型 1.undefined  (var a;) 2.null   (var a=null); 3.String  (var a=" " or ' '); 4.boolea ...

  5. VUE-002-前端分页(el-pagination)展示数据

    在web开发过程中,通常使用表格展示数据,在数据较多时采用分页的方式展示给用户. 分页方式有前端假分页和后端分页两种实现方式,此文仅记录前端假分页实现方式. 第一步:添加分页组件(el-paginat ...

  6. hdfs核心主件服务的启停方式

    停止mapreduce服务 /hadoop/hadoop-2.6.4/sbin/stop-yarn.sh 启动mapreduce服务 /hadoop/hadoop-2.6.4/sbin/start-y ...

  7. 装PIL库

    pip install Pillow -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

  8. Mybatis下的sql注入

    以前只知道mybatis框架下,order by后面接的是列名是不能用#{},这样不起效果,只能用${},这样的话就可能产生sql注入.后来发现其实还有另外两种情况也是类似的: 1.order by ...

  9. java的冒泡排序

    public interface Sorter{ public <T extends Comparable<T>> void sort(T[] list); //定义两个待排序 ...

  10. C#中的反射 Reflection

    起初是因为这个4.0才新增的新的引用类型,我当时很惊讶为什么string和int他都能装,然后我就发在了群里,以为大佬告诉我他的内部实现是通过反射来实现的,于是我就去看反射了,看了网上的好多篇文章大篇 ...