どのようにJavaで結果のリスト自体からプロパティを使用して、ディレクトリの一覧をフィルタ処理するには?

スピリチュアル:

私はと呼ばれるディレクトリ持っている/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());

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=234060&siteId=1