スピリチュアル:
私はと呼ばれるディレクトリ持っている/home/ftp
。このようなサブディレクトリが含まれています。
dir1
dir2
dir3
dir1.ok (this is just an empty file)
dir3.ok (this is just an empty file)
コードは、唯一持っているディレクトリの下のファイルを処理する必要があり、対応する".ok"
ファイルを。例えばのためので。この例では、コードは下にあり、処理のためにそれらのファイルを選ぶ必要がありますdir1
し、dir3
ではなくdir2
。
私は、この「通常」の方法を行うことができます:
List<String> list = Arrays.asList(new File("/home/ftp").list());
Set<String> set = new HashSet<>();
List<String> dirToProcess = new ArrayList<>();
for (String name : list){
name = name.contains(".ok") ? name.substring(0, name.indexOf(".ok")) : name;
if (!set.add(name)){
dirToProcess.add(name);
}
}
// now dirToProcess contains the directory names which should be processed
しかし、私は本当にStreamsを使用して(私はJava8を使用しています)機能のJavaを使用して、これをやりたいです。どのように私はそれを達成するために、このコードをリファクタリングすることができますか?
また:
出力があるように私はあなたが期待しているものだと思います。
List<String> dirToProcess = list.stream()
.filter(name -> name.contains(".ok") && list.contains(name.substring(0, name.indexOf(".ok"))))
.map(name -> name.substring(0, name.indexOf(".ok")))
.collect(Collectors.toList());