Java嵌套流

Java嵌套流,java,java-8,java-stream,Java,Java 8,Java Stream,快速问题: 如何引用嵌套流的父元素,如下所示: jpegDir.stream() .parallel() .map(E -> createDimentionArray(E.getDescription(1), E.getDescription(3))) .filter(E -> E != null) .forEach(E -> images.stream() .filter(J -> J.getType().contains

快速问题: 如何引用嵌套流的父元素,如下所示:

jpegDir.stream()
    .parallel()
    .map(E -> createDimentionArray(E.getDescription(1), E.getDescription(3)))
    .filter(E -> E != null)
    .forEach(E -> images.stream()
        .filter(J -> J.getType().contains(".jpg"))
        .forEach(I -> I.setSize(E))
    );

最后一个
forEach
中的父元素E的类型为
string
。有没有办法在
setSize()
方法中引用E

更新

只需发布固定代码,如果其他人有问题,他们可以查看:

public class Inderxer {

  public void indexFolder(Path path) throws IOException {
      List<Image> images = new LinkedList<>();
       Files.walk(path).parallel().forEach(E -> {
        if (!E.toFile().isDirectory() && !E.toFile().getName().contains(".DS") && FilenameUtils.getExtension(E.toFile().toString()) != ".kmz") {
            try {

                Metadata metadata = ImageMetadataReader.readMetadata(E.toFile());
                if (FilenameUtils.getExtension(E.toFile().toString()).contains("jpg")) {
                    List<Directory> dirs = metadata.getDirectoriesOfType(JpegDirectory.class).stream().collect(Collectors.toList());
                    images.add(new Image(E.getFileName().toString(), FilenameUtils.getExtension(E.toFile().toString()), dirs));
                } else {
                    List<Directory> dirs = metadata.getDirectoriesOfType(PngDirectory.class).stream().collect(Collectors.toList());
                    images.add(new Image(E.getFileName().toString(), FilenameUtils.getExtension(E.toFile().toString()), dirs));
                }

            } catch (ImageProcessingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    images.stream()
            .forEach(M -> M.getDirectory().stream()
                    .forEach(D -> {
                        if (!M.getType().equals("png")) {
                            M.setSize(createDimentionArray(D.getDescription(1), D.getDescription(3), "jpg"));
                        } else {
                            M.setSize(createDimentionArray(D.getDescription(1), D.getDescription(2), "png"));
                        }
                    }));

    images.forEach(System.out::println);
}

private String createDimentionArray(String h, String w, String type) {
    String res = "";
    if (type.equals("jpg")) {
        if (h != null && w != null) {
            String[] hsplit = h.split("pixels");
            String[] wsplit = w.split("pixels");
            if (hsplit[0] != null && wsplit[0] != null) {
                res = hsplit[0]; //Hight
                res += wsplit[0]; //Width
                res += "\n";
            }
            return res;
        } else {
            return null;
        }

    } else {
        if (h != null && w != null) {
            String[] hsplit = h.split("pixels");
            String[] wsplit = w.split("pixels");
            if (hsplit[0] != null && wsplit[0] != null) {
                res = hsplit[0]; //Hight
                res += " ";
                res += wsplit[0]; //Width
                res += "\n";
            }
            return res;
        } else {
            return null;
        }
    }
  }
}
公共类indexer{
public void indexFolder(路径路径)引发IOException{
列表图像=新建LinkedList();
Files.walk(path).parallel().forEach(E->{
如果(!E.toFile().isDirectory()&&&!E.toFile().getName().包含(“.DS”)&&FilenameUtils.getExtension(E.toFile().toString())!=“.kmz”){
试一试{
Metadata Metadata=ImageMetadataReader.readMetadata(E.toFile());
if(FilenameUtils.getExtension(E.toFile().toString()).contains(“jpg”)){
List dirs=metadata.getDirectoriesOfType(JpegDirectory.class).stream().collect(Collectors.toList());
添加(新图像(例如getFileName().toString(),FilenameUtils.getExtension(例如toFile().toString()),dirs));
}否则{
List dirs=metadata.getDirectoriesOfType(PngDirectory.class).stream().collect(Collectors.toList());
添加(新图像(例如getFileName().toString(),FilenameUtils.getExtension(例如toFile().toString()),dirs));
}
}捕获(ImageProcessingException e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
});
images.stream()
.forEach(M->M.getDirectory().stream())
.forEach(D->{
如果(!M.getType().equals(“png”)){
M.setSize(createDimensionArray(D.getDescription(1),D.getDescription(3),“jpg”);
}否则{
M.setSize(CreateDimensionArray(D.getDescription(1),D.getDescription(2),“png”);
}
}));
image.forEach(System.out::println);
}
私有字符串CreateDimensionArray(字符串h、字符串w、字符串类型){
字符串res=“”;
if(类型等于(“jpg”)){
如果(h!=null&&w!=null){
字符串[]hsplit=h.split(“像素”);
字符串[]wsplit=w.split(“像素”);
if(hsplit[0]!=null&&wsplit[0]!=null){
res=hsplit[0];//高
res+=wsplit[0];//宽度
res+=“\n”;
}
返回res;
}否则{
返回null;
}
}否则{
如果(h!=null&&w!=null){
字符串[]hsplit=h.split(“像素”);
字符串[]wsplit=w.split(“像素”);
if(hsplit[0]!=null&&wsplit[0]!=null){
res=hsplit[0];//高
res+=”;
res+=wsplit[0];//宽度
res+=“\n”;
}
返回res;
}否则{
返回null;
}
}
}
}

应该可以按原样工作,您正在
E
的范围内调用
setSize
。然后必须是我的代码的其他部分返回空值。您可以添加
.peek(System.out::println)
调用流以打印流中正在处理的值…请注意,用于返回
CreateDimensionArray(E.getDescription(1),E.getDescription(3))
E
forEach
的lambda参数不同。它的类型是
createdimensionarray
的返回类型。这可能很正常,但用相同的名称命名不同的事物会让人感到困惑。您是否知道
images.stream().filter(J->J.getType().contains(“.jpg”)
的处理结果总是相同的?重复这一操作不仅浪费资源,而且似乎并不正确,因为对于通过过滤器的
jpegDir
的每个元素,您在同一对象上多次调用
setSize
。或者,这与目的相去甚远,或者变量和方法的命名方案有问题。哇。这段代码中有很多错误。使用
equals
比较
String
s。在
管道中引入过滤步骤,而不是在
foreach
中进行过滤。使用
收集器
创建
图像
。此外,这并不是因为您使用的是lambda的方法,您无法再提取方法,请尝试编写声明性代码,并使用意图显示名称,并将内容移动到正确的抽象级别。这很酷。。但除了粗鲁的评论,你们能用一些例子来回答这些“错误”吗?这样你的评论就会变得有建设性。。如果我知道正确的方法,我就不会犯这些“错误”。@Lasse因为这是一个答案而不是问题,评论才是正确的做法。评论中有几个提示可以解决您的问题。也许你应该阅读一些关于
Stream
API()的教程。也在同一资源上。教你不是我们的责任。如果你有一个具体的问题,那么你会得到一个具体的答案。