How to get unique values in List
Asked Answered
G

4

16

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?

Gehenna answered 1/10, 2014 at 22:56 Comment(1)
Don't add duplicate values if the List already contains() it?Farina
W
24

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.

Wien answered 1/10, 2014 at 23:13 Comment(3)
I think you mean: list.stream().distinct().collect(Collectors.toList());Chacha
@Chacha I was young, foolish and in love. Who can blame me for such folly? (thanks - code corrected)Wien
lol your code actually pointed me to the right direction ;) cheersChacha
A
11

Convert the ArrayList to a HashSet.

List<String> listWithDuplicates; // Your list containing duplicates
Set<String> setWithUniqueValues = new HashSet<>(listWithDuplicates);

If for some reason, you want to convert the set back to a list afterwards, you can, but most likely there will be no need.

List<String> listWithUniqueValues = new ArrayList<>(setWithUniqueValues);
Anorak answered 1/10, 2014 at 22:58 Comment(1)
Thnks a lot for you answer.Gehenna
N
3

In Java 8:

     // List with duplicates
     List<String> listAll = Arrays.asList("A", "A", "B", "C", "D", "D");

     // filter the distinct 
     List<String> distinctList = listAll.stream()
                     .distinct()
                     .collect(Collectors.toList());

    System.out.println(distinctList);// prints out: [A, B, C, D]

this will also work with objects, but you will probably have to adapt your equals method.

Nakashima answered 16/11, 2017 at 10:21 Comment(0)
G
0

i just realize a solution may be it can be helpful for other persons. first will be populated with duplicated values from BufferReader.

ArrayList<String> first = new ArrayList<String>();  

To extract Unique values i just create a new ArrayList like down:

ArrayList<String> otherList = new ArrayList<>();

    for(String s : first) {
        if(!otherList.contains(s))
            otherList.add(s);
    }

A lot of post in internet are all speaking to assign my Arraylist to a List , Set , HashTable or TreeSet. Can anyone explain the difference in theory and whitch one is the best tu use in practice ? thnks for your time guys.

Gehenna answered 1/10, 2014 at 23:10 Comment(1)
The Java API documentation provides elaborate documentation for each of the collection classes and interfaces. The Wikipedia page on lists also provides an overview and links to pages about other collection types.Anorak

© 2022 - 2025 — McMap. All rights reserved.