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?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Junior Fulcrum
  • 153
  • 1
  • 1
  • 7

4 Answers4

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
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);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
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.

Jorciney
  • 674
  • 10
  • 11
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.

Junior Fulcrum
  • 153
  • 1
  • 1
  • 7
  • The Java API documentation provides elaborate documentation for each of the collection classes and interfaces. The [Wikipedia page on lists](http://en.wikipedia.org/wiki/List_(abstract_data_type)) also provides an overview and links to pages about other collection types. – Robby Cornelissen Oct 01 '14 at 23:18