[英]How can I clean up my Java Streams?
I have this stream, as well as quite a few others, where I abuse a bunch of calls to get the job done. 我和其他很多人一样,在这里,我滥用了大量的电话来完成工作。 Seems like an anti-pattern when taking advantage of J8. 利用J8时好像是反模式。
+ Arrays.stream(resources)
+ .map(this::ingestFromFile)
+ .collect(Collectors.toList())
+ .stream()
+ .map(Container.class::cast)
+ .map(Container::getAll)
+ .flatMap(Collection::stream)
+ .collect(Collectors.toList())
+ .forEach(
+ dataType -> {
+ loaderConstants.getRepo(dataType.toString()).save(dataType);
+ LOGGER.log(Level.INFO, "Saved: " + dataType);
+ });
How can I shorten this up and what pitfalls should I look out for in the future to avoid this type of development? 我如何缩短时间,为避免此类发展,我将来应该注意哪些陷阱?
Well, you don't need the intermediate .collect(Collectors.toList())
calls as it's unnecessary and causes avoidable overhead. 好吧,您不需要中间的.collect(Collectors.toList())
调用,因为这是不必要的,并且可以避免可避免的开销。
Arrays.stream(resources)
.map(this::ingestFromFile)
.map(Container.class::cast)
.map(Container::getAll)
.flatMap(Collection::stream)
.forEach(
dataType -> {
loaderConstants.getRepo(dataType.toString()).save(dataType);
LOGGER.log(Level.INFO, "Saved: " + dataType);
});
Then you can extract the body of the forEach
to a method to simplify the pipeline. 然后,您可以将forEach
的主体提取为简化管道的方法。
My name is Andy, and I don't like streams. 我叫安迪(Andy),我不喜欢串流。
And this code is a great example of why. 这段代码很好地说明了为什么。 (OK, they are sometimes helpful; but really only in very simple cases). (好的,它们有时会有所帮助;但实际上仅在非常简单的情况下)。
One thing that you (hopefully) learn very early on in programming is that identation makes your code readable: it helps you understand where things happen on related values. 您(很希望)在编程的早期就学到了一件事,即身份使代码易于阅读:它可以帮助您了解相关值发生的事情。
In streamy code, indentation goes right out of the window. 在流式代码中,缩进直接显示在窗口之外。 That left-hand margin is straight - see how all the periods line up: 左边距是笔直的-查看所有期间如何排列:
+ Arrays.stream(resources)
+ .map(this::ingestFromFile)
+ .collect(Collectors.toList())
+ .stream()
+ .map(Container.class::cast)
+ .map(Container::getAll)
+ .flatMap(Collection::stream)
+ .collect(Collectors.toList())
+ .forEach(
+ dataType -> {
+ loaderConstants.getRepo(dataType.toString()).save(dataType);
+ LOGGER.log(Level.INFO, "Saved: " + dataType);
+ });
There is just no visual structure to it. 只是没有视觉结构。
Especially as this is using forEach
, it would just be easier to use loops. 尤其是在使用forEach
,使用循环会更容易。
for (var resource : resources) {
var fromFile = resource.ingestFromFile();
var container = (Container) fromFile;
for (var dataType : container.getAll()) {
loaderConstants.getRepo(dataType.toString()).save(dataType);
LOGGER.log(Level.INFO, "Saved: " + dataType);
}
}
(You may not be using Java 10; but I don't know the data types anyway. Fill in type types instead of var
if you need it). (您可能没有使用Java 10;但是我还是不知道数据类型。如果需要,请填写类型而不是var
)。
Personally, I think that is way easier to read. 就个人而言,我认为这是比较容易的方式阅读。 And it's shorter. 而且更短。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.