2016-11-29 104 views
2

快速的問題: 我如何引用嵌套流的父元素這樣的:Java的嵌套流

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?

+1

這應該是工作,你打電話'setSize'內'E'的範圍。 –

+0

然後,它必須是我的代碼的其他部分返回空值.. – Lasse

+3

您可以添加'.peek(System.out :: println)'調用到您的流打印出流中正在處理的值.. 。 –

回答

0

UPDATE

剛剛發佈的固定代碼,所以如果別人有困難時,他們可以看看:

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; 
     } 
    } 
    } 
} 
+2

只是哇。這段代碼中有很多錯誤。使用'equals'來比較'String's。在'Stream'流水線中引入一個過濾步驟,而不是在'foreach'中過濾。使用'收集器'來創建'圖像'。 – Flown

+0

另外,這並不是因爲你使用的是lambda表達式,你不能再提取方法,試圖編寫聲明性代碼,用意圖顯示名稱並將事物移動到正確的抽象級別。 –

+0

這很酷,所有..但是做出粗魯的評論,你可以回答一些如何解決這些「錯誤」的例子嗎?這樣你的評論就變得有建設性。如果我知道正確的做法,我就不會犯這些「錯誤」。 – Lasse