Best Practices for Getting Started Quickly with Stream Processing | JD Logistics Technical Team

I. Introduction

JAVA1.8 benefits from the functional programming brought by Lambda and introduces a new Stream concept. The Stream concept is similar to the "production assembly line" on the factory floor. The Stream stream is not a data structure and does not save data. It is the processing of data. Stream can be regarded as a process on the assembly line. On the assembly line, a raw material is processed into a product through multiple processes.

Introduction to two commonly used methods

2.1 Get Stream stream

All Collection collections can obtain streams through the stream default method;

The default method stream is added to the java.util.Collection interface to obtain the stream, so all its implementation classes can obtain the stream.

ArrayList<XyBug> xyBugList = new ArrayList();
Stream<XyBug> stream = xyBugList.stream();


The static method of of the Stream interface can obtain the stream corresponding to the array.

//String
Stream<String> stream = Stream.of("aa", "bb", "cc");
//数组
String[] arr = {"aa", "bb", "cc"};
Stream<String> stream7 = Stream.of(arr);
Integer[] arr2 = {11, 22, 33};
Stream<Integer> stream8 = Stream.of(arr2);
//对象
XyBug xyBug1 = new XyBug();
XyBug xyBug2 = new XyBug();
XyBug xyBug3 = new XyBug();
Stream<XyBug> bugStream = Stream.of(xyBug1, xyBug2, xyBug3);



2.2 Common methods of Stream data processing

forEach method

This method receives a Consumer interface function and will hand over each stream element to this function for processing.

List<String> list = new ArrayList<>();
Collections.addAll(list, "str1", "str2", "str3", "str4", "str5", "str6");
list.stream().forEach((String s) -> {
  System.out.println(s);
  });
//简写
list.stream().forEach(s -> System.out.println(s));


s represents each element in the list, and streaming processing traverses each element in turn

-> The code after that handles the logic for each element

count method

The count method counts the number of elements in it, and the return value is of long type.

long count = list.stream().count();


distinct method

Perform deduplication operations on data in the stream. Common types can be deduplicated directly.

//将22、33重复数据去除
Stream.of(22, 33, 22, 11, 33).distinct().collect(Collectors.toList());


Custom types remove duplicate elements based on the hashCode and equals of the object.

Add the @Data annotation to the XyBug entity class, hashCode and equals will be rewritten separately, and deduplication will be determined when using the distinct method.

ArrayList bugList = JSON.parseObject(bugs, ArrayList.class);
ArrayList<XyBug> xyBugList = new ArrayList();
List collect = (List) bugList.stream().distinct().collect(Collectors.toList());


Use the distinct() method to remove duplication, and use collect(Collectors.toList()) to form a new list of 6 after deduplication.

limit method

The method can intercept the stream and only use the first n items. The parameter is a long type. If the current length of the collection is greater than the parameter, it will be intercepted. Otherwise no operation will be performed

List<String> list = new ArrayList<>();
Collections.addAll(list, "1", "2", "3", "4", "5", "6");
List<String> collect = list.stream().limit(3).collect(Collectors.toList());


Intercept the first 3 String objects to form a new list

skip method

If you want to skip the first few elements, you can use the skip method to obtain a new stream after interception. If the current length of the stream is greater than n, skip the first n elements; otherwise, you will get an empty stream with a length of 0

List<String> list = new ArrayList<>();
Collections.addAll(list, "1", "2", "3", "4", "5", "6");
List<String> collect = list.stream().skip(3).collect(Collectors.toList());


Skip the first three String objects and form a new list with the last three

filter method

filter is used to filter data and return data that meets the filtering conditions. You can convert a stream into another subset stream through the filter method. This interface receives a Predicate functional interface parameter (which can be a Lambda or method reference) as the filtering condition.

List<String> list = new ArrayList<>();
Collections.addAll(list, "1", "22", "3", "4", "55", "6");
//filter方法中写入筛选条件,将过滤后的数据组成新的list
list.stream().filter(s -> s.length() == 2).collect(Collectors.toList());


Through this statement s -> s.length() == 2, 22 and 55 are filtered out

map method

Mapping elements in a stream to another stream can convert T type data in the current stream to another R type stream

List<PersonCrDto> laputaCrDtos = queryListLaputaByBeginEndTime(begin, end);
//将list中的PersonCrDto对象的userName属性取到,收集成set集合
laputaCrDtos.stream().map(PersonCrDto::getUserName).collect(Collectors.toSet())


Get the userName data of each object in the list to form a Set collection

stream grouping

List<XyBug> list = new ArrayList<>();
Map<String, List<XyBug>> collect = list.stream().collect(Collectors.groupingBy(XyBug::getBugType));


Group according to the bug type. After grouping, a map will be formed. The key is the group name and the value is the data under the group.

stream sorting

sort(), the default is forward order, add the reversed() method to sort backwards

List<XyBug> list = new ArrayList<>();
//根据createTime正序排列
List<XyBug> collect = list.stream().sorted(Comparator.comparing(XyBug::getCreateTime)).collect(Collectors.toList());
//根据createTime倒叙排列
List<XyBug> collect = list.stream().sorted(Comparator.comparing(XyBug::getCreateTime).reversed()).collect(Collectors.toList());



collect method

Collect processed data into a list, collect(Collectors.toList())

Collect processed data as set, collect(Collectors.toSet())

Group data according to a certain field value map, collect (Collectors.groupingBy(o -> o.value())))

Three practical examples

Requirement: Group bug data by orgTierName and store it in the map

Stream is not used, you need to use a for loop and make various judgments, and the number of lines of code is large.

HashMap<String, List<XyBug>> map = new HashMap<>();
for (XyBug one : bugList){
    if(one.getOrgTierName() != null){
        if(map.get(one.getOrgTierName()) == null){
            List<XyBug> list = new ArrayList();
            list.add(one);
            map.put(one.getOrgTierName(),list);
        }else {
            map.get(one.getOrgTierName()).add(one);
        }
    }
}


Using Stream, it can be done in one line of code , intuitive and efficient.

collectDeptBugMap = bugList.stream().filter(o -> o.getOrgTierName() != null).collect(Collectors.groupingBy(o -> o.getOrgTierName()));


Four summary

Stream is an enhancement to the function of Collection objects. It can perform various very convenient and efficient aggregation operations on collection objects, or perform large-scale data operations, improving programming efficiency, simplicity and program readability. Through simple examples, this article hopes to help readers quickly get started using stream processing. The Stream processing function is very powerful. For more methods, please refer to the API documentation.

Author: JD Logistics Yang Jingping

Source: JD Cloud Developer Community Ziyuanqishuo Tech Please indicate the source when reprinting

Bun releases official version 1.0, a magical bug in Windows File Explorer when JavaScript is runtime written by Zig , improves performance in one second JetBrains releases Rust IDE: RustRover PHP latest statistics: market share exceeds 70%, the king of CMS transplants Python programs To Mojo, the performance is increased by 250 times and the speed is faster than C. The performance of .NET 8 is greatly improved, far ahead of .NET 7. Comparison of the three major runtimes of JS: Deno, Bun and Node.js Visual Studio Code 1.82 NetEase Fuxi responded to employees "due to BUG was threatened by HR and passed away. The Unity engine will charge based on the number of game installations (runtime fee) starting next year.
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4090830/blog/10110405