2014-07-03 26 views
2

我想這樣做:如何在IntStream上調用map()並返回其他類型?

  IntStream.range(0, fileNames.size()) 
       .map(i -> "mvn deploy:deploy-file" + 
         " -DrepositoryId=" + REPO_ID + 
         " -Durl=" + REPO_URL + 
         " -Dfile=" + LIBS + fileNames.get(i) + 
         " -DgroupId=" + GROUP_ID + 
         " -DartifactId=" + artifactName.get(i) + 
         " -Dversion=" + versionNumbers.get(i) + 
         " -DgeneratePom=true;") 
       .collect(Collectors.toList()); 

但是,這並不編譯,因爲map()通行證在int並返回int。我如何從int映射到String

PS:有沒有更習慣的方式來編寫這段代碼?

回答

2

IntStream對此有一個mapToObj方法。有了它的仿製藥,無需投射:

 IntStream.range(0, fileNames.size()) 
       .mapToObj(i -> "mvn deploy:deploy-file" + 
         " -DrepositoryId=" + REPO_ID + 
         " -Durl=" + REPO_URL + 
         " -Dfile=" + LIBS + fileNames.get(i) + 
         " -DgroupId=" + GROUP_ID + 
         " -DartifactId=" + artifactName.get(i) + 
         " -Dversion=" + versionNumbers.get(i) + 
         " -DgeneratePom=true") 
       .map(s -> s + ";") 
       .collect(Collectors.toList()); 
相關問題