Java8 API unfriendly Stream? With IntelliJ IDEA to optimize it

table of Contents

Unfriendliness of Java8 Stream API's

With IntelliJ IDEA to optimize it

2.1 New Stream Live Templates

2.2 shoving the other commonly used templates set about

Unfriendliness of Java8 Stream API's

Java8 Since its launch in March 2014, has passed a number of years. Within the company has gradually migrate all the production code to this version. Migration work is mainly done is to update the code base as lambda expressions, Stream flow and a new date API. It also uses dynamic Nashorn write those functions may need to be changed at runtime.

In addition to the lambda expression, the most common feature is a new Stream API, and it can be used to easily set of operations. In addition, Stream can be greatly improved readability of the code related to the operation of the set.

However, with regard to Stream flow, there is one thing that really bothers me!

Stream flow in the use of such a terminal similar findFirst and reduce operational semantics are simple and clear, and very direct, we no longer need to pass additional parameters.

Another and more we are using the terminal operation: collect used up very cumbersome, as we want to convert into a List, we generally this wording: .collect (Collectors.toList ()).

For example, the following code to set the string filter, and transferred into a new collection:

stringCollection

.stream()

.filter(e -> e.startsWith(

"a"

))

.collect(

Collectors

.toList());

As another example, we have to be converted to Set the time, you need to write this: .collect (Collectors.toSet ()), as well as joining groupingBy and the like.

After I completed the migration of 300,000 lines of code stream of steam, I summed up what, toList, toSet and groupingBy our project is the most common terminal operations. I can not understand JDK designers why not collect method again simplify, to make it more humane some, such as the same piece of code, why not designed as follows:

stringCollection

.stream()

.filter(e -> e.startsWith(

"a"

))

.toList();

Because it not designed in such a way, cause we had to use over and over again Collectors.toXXX in the code, which is irritable.

With IDEA about optimization

IntelliJ IDEA universe known as the most intelligent Java IDE, see how we solve this problem of unhappy people through it.

IntelliJ IDEA comes with a handy feature called LiveTemplates (real-time template) is. This is a hell? Even if you do not know what it is, but you must also frequently used to it. Such as input shortcut common code segments sout, IDEA inserted snippet System.out.println (). As another example, input psvm, IDEA will quickly help you generate main methods.

17130423-4926c9bbae8d4a88

Well, on this thing?

It is up to how to solve the pain points mentioned above it by LiveTemplates (real-time template)?

1 New Stream Live Templates

Step 1. Press Open  File -> Setting -> Editor -> LiveTemplates :

17130423-5aaa7027349507d8

2. Create a  Template Group  , named  Stream :

17130423-7ea8716bff1cae8d

3. Once created, select the Stream, a new live templates:

17130423-177f62934ddf187c

4. Create a template toList snippet:

17130423-b0ca5777627115fe

Note: context code fragment selected from Java-> other.

5. After setup is complete, let us look at the effect in actual combat it!

17130423-5a733bc7d618bba2

2 shoving the other commonly used templates set about

In addition to the presentation of the above toList, toSet, groupingBy join and use is also quite common, we have to set about the method similar to the above, here it is not a map, paste the code snippets:

// abbreviated: .toList

.collect(

Collectors

.toList());

// abbreviated: .toSet

.collect(

Collectors

.toSet());

// abbreviated: .join

.collect(

Collectors

.joining(

"$END$"

));

// abbreviated: .groupBy

.collect(

Collectors

.groupingBy(e -> $END$));

PS: the END $ $ special variable used to generate the snippet, the cursor position is determined, so you can enter the code directly related to, for example, the definition of join connecting the separator and the like.

Templates are created later, shots are as follows:

17130423-0e8d1901b97572fd

Finally, let us look, join and use renderings groupBy:

17130423-a45501c57399f74e
17130423-f88414c4894d5632

Reproduced in: https: //www.jianshu.com/p/1a8d25a35261

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/91163523