0

Is it possible to get all the distinct values in an arraylist or hashmap?

Let's say for example I have elements on arraylist like this :

123, 123, 456, 123, 456, 000

And in HashMap :

123, test1
123, test2
456, test3
123, test4
000, test5
4
  • 3
    Use a Set in that case; I don't know how are you able to store 123 again as a key in a Map Commented Jun 17, 2015 at 12:45
  • I'm just assuming if it's possible to put duplicate Keys in HashMap. Commented Jun 17, 2015 at 12:48
  • Sets (like HashSet) are collections of distinct elements. Commented Jun 17, 2015 at 12:50
  • HashMap can't have duplicate values for same key. User guava MultiMap for that behavior. Commented Jun 17, 2015 at 12:51

5 Answers 5

4

Keys should be unique in HashMap. if you are choosing duplicate keys then existing key value will be replace with new value.

if you want to avoid duplicate then use Set.

Sign up to request clarification or add additional context in comments.

4 Comments

What will happen if I'm gonna read a text file and put it on HashMap, my text file has multiple data.
In HashMap it will override last value so you will able to see last value you have added for the same key.
So in short, the existing key will be replaced with another key that same as that key. Right?
Existing key is replaced with new one. That means you lost your existing key data.
0

If you are using a Map, by definition the keys will be unique. For implementations of List there are a few options. For Java 5 through Java 7

public <T> List<T> removeDuplicates(List<T> list){
    Set<T> set = new LinkedHashSet<>(list);
    return new ArrayList<>(set);
}

With Java 8

public <T> List<T> removeDuplicatesJava8(List<T> list){
    return list.stream().distinct().collect(Collectors.toList());
}

4 Comments

How about with Java 6?
see the first example. "pre-java8" means anything before java 8. That would include java 6.
Oh oops sorry, I didnt see that one.
Thank you for explaining this @MadConan. So it's better to use Java 8 because it has distinct method.
0

HashMaps don't allow duplicate keys. If you enter a key which is already there then it replaces it with the new one.

Comments

0

To get a Collection of unique elements from a List you can do that

Set<Object> set = new HashSet<Object>(nonUniqueList);

Comments

0

You could use a Map<K, List<V>> (or Map<K, V[]>) if you like to map multiple values to a key:

Code:

public static void main(String[] args) {
    Map<Integer, List<String>> data = new HashMap<>();
    add(data, 123, "test1");
    add(data, 123, "test2");
    add(data, 456, "test3");
    add(data, 123, "test4");
    add(data,   0, "test5");
    data.forEach((k, v) -> System.out.println(k + " -> " + v));
}

static <K, V> void add(Map<K, List<V>> listMap, K key, V value) {
    List<V> values = listMap.get(key);
    if(values == null) {
        values = new ArrayList<V>();
        listMap.put(key, values);
    }
    values.add(value);
}

Output:

0 -> [test5]
456 -> [test3]
123 -> [test1, test2, test4]

2 Comments

Awesome! Thank you for this one, I understand now how Set and HashMap works!
@tuturyokgaming You should upvote any answer that helped you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.