Some common operations of Stream

Some operations of Stream

It's really convenient to use Java8 stream operation in project development. Here are some common stream operations

1 Introduction

Before using Stream flow, let's briefly introduce:

Stream is a means of processing data sets in stream mode provided by Java 8. Collection can be used for all word classes or sub interfaces under the collection interface Stream () method to obtain a stream object;

2 Java 8 API mentioned that each Colection Stream can only be executed once. If you want to execute it again, you can only recreate the creation, because the Stream stream will automatically close the Stream after execution by default

(from this point, we can find that the Stream interface extends the BaseStream interface, and the BaseStream interface extends the autocolseal interface, so we don't need to manually close the Stream after each use.)

3 Stream shields the traversal mode of a certain element in the direct operation set, but pays more attention to the aggregation operation calculation of the element set (such as grouping, summation, sorting, filtering, etc.);

4 although we can't directly manipulate an element, we can use basestream The iterator () method iterates over each element, using basestream Splicing of elements by splitter () method;

If you look at the API, you may feel a little confused, but in fact, we can simply understand it

Stream is a tool for aggregating elements in the Collection. We can use stream to complete almost all aggregation function operations and filtering operations on the SQL level;

Using Stream streams avoids the trouble of using loops to manipulate elements and then aggregate them;

This roughly means that if you are comfortable with using Stream stream, your code efficiency and time efficiency will be greatly improved. Therefore, it is a good choice to first consider whether you can use Stream stream to complete before operating collection elements, which may avoid many mistakes;

Of course, if you jdk don't support when I don't say!!!

2 common operations

There are no more examples and detailed descriptions of common operations. They are all API s. I only write examples. As long as I have a little java foundation, I can understand what they mean

2.1 grouping according to a field

Map<String, List<IotHzsStB>> listMap = list.stream().collect(Collectors.groupingBy(IotHzsStB::getSttp));

2.2 extract a field

List<Long> collect = menuList.stream().map(SysMenu::getId).collect(Collectors.toList());

2.3 filtration

Long tYellAlarmNum = iotAlarmS.stream().filter(item -> item.getAlarmType().equals(ConstantEnum.IOT_ALARM_TYPE_1.getValue())).filter(item -> item.getLevel().equals(ConstantEnum.IOT_ALARM_LEVEL_0.getValue())).count();

2.4 sorting (multi field)

returnList = returnList.stream().sorted(Comparator.comparing(IotPpJcResponse::getDrp,Comparator.reverseOrder()).thenComparing(IotPpJcResponse::getAddvcd,Comparator.reverseOrder())).collect(Collectors.toList());

2.5 take out the first element in the first set

list.stream().findFirst().get();

2.6 match (one match is true)

boolean isAdmin = sysUserRoleList.stream().anyMatch(r -> "admin".equalsIgnoreCase(r.getRoleCode()));

2.7 match (all elements match and return true)

boolean isAdmin = sysUserRoleList.stream().allMatch(r -> "admin".equalsIgnoreCase(r.getRoleCode()));

2.8 match (return true if none match)

boolean isAdmin = sysUserRoleList.stream().noneMatch(r -> "admin".equalsIgnoreCase(r.getRoleCode()));

3 conclusion

That's all for the time being, because I've seen these more times. Of course, there are many common operations that are not recorded. I'll supplement them later

Some of the above operations may involve many sub operations, such as count() statistics used in filtering
Of course, you can also check the API documentation of Java 8 to learn more. You can use it at any time.
https://www.matools.com/api/java8

Keywords: Java Back-end

Added by mfindlay on Fri, 24 Dec 2021 02:26:39 +0200