With the new features of java8, you can use simple and efficient code to achieve some data processing. Define 1 Apple object:

public class Apple { private Integer id; private String name; private BigDecimal money; private Integer num; public Apple(Integer id, String name, BigDecimal money, Integer num) { this.id = id; this.name = name; this.money = money; this.num = num; }}Copy the code

Add some test data:

List<Apple> appleList = new ArrayList<>(); // Store apple object set Apple Apple1 = new Apple(1," apple1 ",new BigDecimal("3.25"),10); Apple apple12 = new Apple(1," Apple 2", New BigDecimal("1.35"),20); Apple apple2 = new Apple(2," banana ",new BigDecimal("2.89"),30); Apple apple3 = new Apple(3," litchi ", New BigDecimal("9.99"),40); appleList.add(apple1); appleList.add(apple12); appleList.add(apple2); appleList.add(apple3);Copy the code

1, grouping

Group object elements in a List by an attribute, for example, by id, and group them together with the same ID:

Map<Integer,List<Apple>> Map<Integer, List<Apple>> groupBy = appleList.stream().collect(Collectors.groupingBy(Apple::getId)); System.err.println("groupBy:"+groupBy); {1 = [Apple {id = 1, name = '1' Apple, money = 3.25, num = 10}, Apple {id = 1, name = '2' Apple, money = 1.35, num = 20}], 2 = [Apple {id = 2, Name = 'banana', money = 2.89, num = 30}], 3 = [Apple {id = 3, name = 'lychee', money = 9.99, num = 40}]}Copy the code

2. Convert List to Map

Id = key and apple = value

/** * List -> Map * toMap Duplicate key.... * Apple1 and apple12 both have ids of 1. * You can set (k1,k2)->k1, If there are duplicate keys, keep key1 and discard key2 */ Map<Integer, Apple> appleMap = applelist.stream ().collect(Collectors. ToMap (Apple::getId, a -> a,(k1,k2)->k1));Copy the code

Print appleMap

1 = {{Apple id = 1, name = '1' Apple, money = 3.25, num = 10}, 2 = Apple {id = 2, name = 'banana', money = 2.89, num = 30}, 3 = {Apple id = 3, name = 'lychee', Money = 9.99, num = 40}}Copy the code

3. Filter

Filter out qualifying elements from the collection:

List<Apple> filterList = applelist.stream ().filter(a -> Atul gawande etName (.) the equals (" banana ")). Collect (Collectors. ToList ()); System.err.println("filterList:"+filterList); [Apple{id=2, name=' banana ', money=2.89, num=30]Copy the code

4, sum

Sum the data in the collection according to an attribute:

// Calculate the total amount BigDecimal totalMoney = applelist.stream ().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add); System.err.println("totalMoney:"+totalMoney); / / totalMoney: 17.48Copy the code

5. Find the maximum and minimum values in the stream

MaxBy and Collectors. MinBy to calculate the maximum or minimum value in the flow. Search Java bosom friend public account, reply “backend interview”, send you a Java interview questions treasure book

Optional<Dish> maxDish = Dish.menu.stream().
      collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));
maxDish.ifPresent(System.out::println);

Optional<Dish> minDish = Dish.menu.stream().
      collect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));
minDish.ifPresent(System.out::println);
Copy the code

6, to heavy

import static java.util.Comparator.comparingLong; import static java.util.stream.Collectors.collectingAndThen; import static java.util.stream.Collectors.toCollection; List<Person> unique = applelist.stream ().collect(collectingAndThen(toCollection() -> new) TreeSet<>(comparingLong(Apple::getId))), ArrayList::new) );Copy the code

The following table shows static factory methods for Collectors.

Source: blog.csdn.net/lu930124/article/details/77595585

Welcome to follow my wechat public account “Code farming breakthrough”, share Python, Java, big data, machine learning, artificial intelligence and other technologies, pay attention to code farming technology improvement, career breakthrough, thinking transition, 200,000 + code farming growth charge first stop, accompany you have a dream to grow together