开发者

Save a list of unique Strings in the ArrayList

开发者 https://www.devze.com 2022-12-20 04:57 出处:网络
I read dat开发者_如何学Goa from a text file, so there may be: John Mary John Leeds I now need to get 3 unique elements in the ArrayList, because there are only 3 unique values in the file output (

I read dat开发者_如何学Goa from a text file, so there may be:

John
Mary
John
Leeds

I now need to get 3 unique elements in the ArrayList, because there are only 3 unique values in the file output (as above).

I can use a HashTable and add information to it, then simply copy its data into the List. Are there other solutions?


Why do you need to store it in a List? Do you actually require the data to be ordered or support index-based look-ups?

I would suggest storing the data in a Set. If ordering is unimportant you should use HashSet. However, if you wish to preserve ordering you could use LinkedHashSet.


If you have a List containing duplicates, and you want a List without, you could do:

List<String> newList = new ArrayList<String>(new HashSet<String>(oldList));

That is, wrap the old list into a set to remove duplicates and wrap that set in a list again.


You can check list.contains() before adding.

if(!list.contains(value)) {
    list.add(value);
}

I guessed it would be obvious! However, adding items to a HashSet and then creating a list from this set would be more efficient.


Use a set instead of a list. Take a look at here: Java Collections Tutorials and specifically about Sets here: Java Sets Tutorial

In a nutshell, sets contain one of something. Perfect :)


Here is how I solved it:

import groovy.io.*;
def arr = ["5", "5", "7", "6", "7", "8", "0"]
List<String> uniqueList = new ArrayList<String>(new HashSet<String>( arr.asList() ));
System.out.println( uniqueList )


Another approach would be to use Java 8 stream's distinct

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

// public static void main(String args[]) ...

// list of strings, including some nulls and blanks as well ;)
List<String> list = Arrays.asList("John", "Mary", "John", "Leeds", 
null, "", "A", "B", "C", "D", "A", "A", "B", "C", "", null);
 
// collect distinct without duplicates
List<String> distinctElements = list.stream()
                        .distinct()
                        .collect(Collectors.toList());
 
// unique elements
System.out.println(distinctElements);

Output:

 [John, Mary, Leeds, null, , A, B, C, D]


 List<String> distinctElements = list.stream()
            .distinct().filter(s -> s != null && s != "")
            .collect(Collectors.toList());

This will collect the distinct items and also avoid null or empty String


class HashSetList<T extends Object>
    extends ArrayList<T> {

    private HashSet<Integer> _this = new HashSet<>();

    @Override
    public boolean add(T obj) {
        if (_this.add(obj.hashCode())) {
            super.add(obj);
            return true;
        }
        return false;
    }
}

I now use those kind of structure for little programs, I mean you have little overhead in order to have getters and setters but uniqueness. Moreover you can override hashCode to decide wether your item equals another one.

0

精彩评论

暂无评论...
验证码 换一张
取 消