使用Commons或Guava将文本文件转换为Java Set <String>

Mat*_* B. 3 java hashset text-files guava

我想将文件中的每一行加载到HashSet集合中.有一个简单的方法吗?

Sim*_*son 13

怎么样:

Sets.newHashSet(Files.readLines(file, charSet));
Run Code Online (Sandbox Code Playgroud)

(使用番石榴).

参考文献:


Jef*_*ter 10

你可以做

Set<String> lines = new HashSet<String>(FileUtils.readLines(new File("foo.txt")));
Run Code Online (Sandbox Code Playgroud)

使用Apache Commons FileUtils类和readlines方法.