Java 8 Streams Collectors uses

Introduction

In this article, we will show you how to use Collectors of Java 8 streams to group, count, sum and sort lists.

1. Grouping, Counting and Sorting

  1. Grouping by list and displaying the total number of lists.
 List<String> items = Arrays.asList("apple", "apple", "banana",
        "apple", "orange", "banana", "papaya");

Map<String, Long> result = items.stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    System.out.println(result);

Output:

{
    papaya=1, orange=1, banana=2, apple=3
}
  1. Add sort
List<String> items = Arrays.asList("apple", "apple", "banana",
        "apple", "orange", "banana", "papaya");

Map<String, Long> result = items.stream()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));

    Map<String, Long> finalMap = new LinkedHashMap<>();

    result.entrySet().stream()
        .sorted(Entry.<String,Long>comparingByValue().reversed()).forEachOrdered(e->finalMap.put(e.getKey(),e.getValue()));

    System.out.println(finalMap);

Output:

{
    apple=3, banana=2, papaya=1, orange=1
}

2. Listing objects

An example of grouping by user-defined list of objects.

  1. Grouped by name and counted by number or sum.
public class Item {

    private String name;
    private int qty;
    private BigDecimal price;

    //constructors, getter/setters 
}
List<Item> items = Arrays.asList(
        new Item("apple", 10, new BigDecimal("9.99")),
        new Item("banana", 20, new BigDecimal("19.99")),
        new Item("orang", 10, new BigDecimal("29.99")),
        new Item("watermelon", 10, new BigDecimal("29.99")),
        new Item("papaya", 20, new BigDecimal("9.99")),
        new Item("apple", 10, new BigDecimal("9.99")),
        new Item("banana", 10, new BigDecimal("19.99")),
        new Item("apple", 20, new BigDecimal("9.99"))
    );

    Map<String, Long> couting = items.stream()
        .collect(Collectors.groupingBy(Item::getName, Collectors.counting()));

    System.out.println(couting);

    System.out.println("======");

    Map<String, Integer> sum = items.stream()
        .collect(Collectors.groupingBy(Item::getName, Collectors.summingInt(Item::getQty)));

    System.out.println(sum);

Output:

{papaya=1, banana=2, apple=3, orang=1, watermelon=1}
======
{papaya=20, banana=30, apple=40, orang=10, watermelon=10}
  1. Grouping by price, using Collectors. grouping By and Collectors. mapping:
 List<Item> items = Arrays.asList(
        new Item("apple", 10, new BigDecimal("9.99")),
        new Item("banana", 20, new BigDecimal("19.99")),
        new Item("orang", 10, new BigDecimal("29.99")),
        new Item("watermelon", 10, new BigDecimal("29.99")),
        new Item("papaya", 20, new BigDecimal("9.99")),
        new Item("apple", 10, new BigDecimal("9.99")),
        new Item("banana", 10, new BigDecimal("19.99")),
        new Item("apple", 20, new BigDecimal("9.99"))
    );

System.out.println("=====>group by price:");
    // group by price
    Map<BigDecimal, List<Item>> groupByPriceMap = items.stream()
        .collect(Collectors.groupingBy(Item::getPrice));

    System.out.println(groupByPriceMap);

    Map<BigDecimal, Set<String>> collect = items.stream().collect(Collectors
        .groupingBy(Item::getPrice, Collectors.mapping(Item::getName, Collectors.toSet())));

    System.out.println("=====>group by + mapping to Set:");
    System.out.println(collect);

Output:

=====>group by price:
{   19.99=[
        Item{name='banana', qty=20, price=19.99}, 
        Item{name='banana', qty=10, price=19.99}
        ], 
    29.99=[
        Item{name='orang', qty=10, price=29.99}, 
        Item{name='watermelon', qty=10, price=29.99}
        ], 
    9.99=[
        Item{name='apple', qty=10, price=9.99}, 
        Item{name='papaya', qty=20, price=9.99}, 
        Item{name='apple', qty=10, price=9.99}, 
        Item{name='apple', qty=20, price=9.99}
        ]
}
=====>group by + mapping to Set:
{   19.99=[banana], 
    29.99=[orang, watermelon], 
    9.99=[papaya, apple]
}

See: java-8-demo

For more details, see: Java 8 Tutorial

Tags: Java

Posted on Sun, 06 Oct 2019 12:19:39 -0700 by rommel_toledo