Mahedi Hasan
Mahedi Hasan

Reputation: 61

How to print keys with duplicate values in a hashmap?

I have a hashmap with some keys pointing to same values. I want to find all the values that are equal and print the corresponding keys.

This is the current code that I have:

    Map<String, String> map = new HashMap<>();

    map.put("hello", "0123");
    map.put("hola", "0123");
    map.put("kosta", "0123");
    map.put("da", "03");
    map.put("notda", "013");

    map.put("twins2", "01");
    map.put("twins22", "01");


    List<String> myList = new ArrayList<>();

    for (Map.Entry<String, String> entry : map.entrySet()) {
       for (Map.Entry<String, String> entry2 : map.entrySet()){
           if (entry.getValue().equals(entry2.getValue()))
           {
               myList.add(entry.getKey());
           }
       }

    }

The current code adds the duplicates two times into the list, however it also adds every key one time.

Thanks.

Upvotes: 3

Views: 6045

Answers (4)

TongChen
TongChen

Reputation: 1430

I think other answers already good to solve the question, i support another method to do just for extended thinking.This method need use Guava's MutliMap interface:

    // init the input map
    Map<String, String> map = new HashMap<>();
    map.put("hello", "0123");
    map.put("hola", "0123");
    map.put("kosta", "0123");
    map.put("da", "03");
    map.put("notda", "013");
    map.put("twins2", "01");
    map.put("twins22", "01");

    // swap key and value of the input map,since different key has same value
    // so we need Multimap
    Multimap<String, String> container = ArrayListMultimap.create();
    map.entrySet().forEach(entry -> container.put(entry.getValue(), entry.getKey()));

    container.keySet().stream()
        .filter(s -> container.get(s).size() > 1).
        forEach(System.out::println);

output:
01
0123

Upvotes: 0

kemalturgul
kemalturgul

Reputation: 55

If you want a solution beside to Stream API;

    public static void duplicatedValuesMap() {
        Map<String, String> map = new HashMap<>();

        map.put("hello", "0123");
        map.put("hola", "0123");
        map.put("kosta", "0123 test");
        map.put("da", "03");
        map.put("notda", "013");
        map.put("twins2", "01");
        map.put("twins22", "01");

        HashMap<String, List<String>> valueToKeyMapCounter = new HashMap<>();

        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (valueToKeyMapCounter.containsKey(entry.getValue())) {
                valueToKeyMapCounter.get(entry.getValue()).add(entry.getKey());
            } else {
                List<String> keys = new ArrayList<>();
                keys.add(entry.getKey());
                valueToKeyMapCounter.put(entry.getValue(), keys);
            }
        }
        for (Map.Entry<String, List<String>> counterEntry : valueToKeyMapCounter.entrySet()) {
            if (counterEntry.getValue().size() > 1) {
                System.out.println("Duplicated Value:" + counterEntry.getKey() + " for Keys:" + counterEntry.getValue());
            }
        }

    }

Upvotes: 0

Andreas
Andreas

Reputation: 159250

Build a Map<VALUE, List<KEY>>, i.e. a Map<String, List<String>>.

Example

Map<String, String> map = new HashMap<>();
map.put("hello", "0123");
map.put("hola", "0123");
map.put("kosta", "0123");
map.put("da", "03");
map.put("notda", "013");
map.put("twins2", "01");
map.put("twins22", "01");

map.entrySet().stream()
   .collect(Collectors.groupingBy(Entry::getValue,
               Collectors.mapping(Entry::getKey, Collectors.toList())))
   .entrySet().stream()
   .filter(e -> e.getValue().size() > 1)
   .forEach(System.out::println);

Output

01=[twins22, twins2]
0123=[kosta, hello, hola]

Without the filter(), the result would be:

01=[twins22, twins2]
013=[notda]
03=[da]
0123=[kosta, hello, hola]

Upvotes: 0

Tom
Tom

Reputation: 224

You can use streams to retrive duplicates in this way:

  List<String> myList = map.stream()
     .filter(n -> Collections.frequency(map.values(), n) > 1)
     .collect(Collectors.toList());

And then, you can print this out with:

myList.foreach(System.out::println);

Upvotes: 2

Related Questions