I have 2 text files with data. I am reading these files with BufferReader
and putting the data of one column per file in a List<String>
.
I have duplicated data in each one, but I need to have unique data in the first List
to confront with the duplicated data in the second List
.
How can I get unique values from a List
?
It can be done one one line by using an intermediate Set
:
List<String> list = new ArrayList<>(new HashSet<>(list));
In java 8, use distinct()
on a stream:
List<String> list = list.stream().distinct().collect(Collectors.toList());
Alternatively, don't use a List at all; just use a Set (like HashSet) from the start for the collection you only want to hold unique values.