Text file in Java Set <String> using Commons or Guava

I would like to upload each line to a file in the HashSet collection. Is there an easy way to do this?

+3
source share
4 answers

What about:

Sets.newHashSet(Files.readLines(file, charSet)); 

(using guava).

Literature:

+13
source

You can do

 Set<String> lines = new HashSet<String>(FileUtils.readLines(new File("foo.txt"))); 

Using the Apache Commons FileUtils and readlines Class

+9
source

Multiset can store duplicate lines if your text contains duplicate lines. (add order)

 Multiset<String> set = LinkedHashMultiset.create(); 
+2
source

With Apache Commons IO , you have readLines which returns a List . Then you can add all the elements from the returned list to the HashSet (be careful: combine the type between List and Set and lose duplicate lines).

0
source

Source: https://habr.com/ru/post/952696/


All Articles