0

I have been reviewing the following question but I'm still a bit confused. How may I loop through files, looping through the contents of each file, adding only the distinct values from ALL the files to one List? Should I use a set?

getSourceFiles returns a LinkedHashMap of Paths which I then cast to File objects

    public static void main(String[] args) throws IOException {

mapSwift = getSourceFiles(PATH_SWIFT, swiftFileName);
linkedHashSet = new LinkedHashSet();
mapSwift.forEach((k,v)-> {
        File fileName = new File(v.toString());
        try (Stream<String> stream = Files.lines(Paths.get(String.valueOf(fileName)))) {
            list = stream
                    .filter(line -> line.startsWith(":97A::SAFE//"))
                    .map(String::toUpperCase)
                    .distinct()
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }

    linkedHashSet.add(list);

});


    System.out.println("Done");

}
//public static LinkedHashMap<?, ?> getSourceFiles(Path dir, String strFilename)

Expected Output

File A | File B | File C
Oranges Apples Bananas
Apples  Apples Oranges

I would like the resulting Array or Set to have only unique vales from ALL files. With the above example, the ending data structure would only contain 3 elements, Oranges, Apples, Bananas

shmosel
  • 49,289
  • 6
  • 73
  • 138
phillipsK
  • 1,466
  • 5
  • 29
  • 43
  • 1
    show some sample input from file and what is actual output that you are getting now – Ryuzaki L Apr 12 '19 at 00:46
  • Just keep on collecting them to a `Set` and you would not have duplicates in your resulting collection. – Naman Apr 12 '19 at 01:44
  • Just consider that if you don’t show us your variable declarations, we don’t know how you declared them. Well, you should generally declare locally used variables as local variables and not somewhere else. Besides that, omit these strange detours like doing `fileName = new File(v.toString())`, just to do `String.valueOf(fileName)` afterwards. – Holger Apr 12 '19 at 08:56

0 Answers0