简体   繁体   中英

How to find keys in the HashMap<String, LinkedList<String>> which have one or more similar values in LinkedList<String>

So I am trying to solve the following problem, I used HashMap as DS to Store the input.

Question: Given an address book contains a list of contacts which were imported from different sources (outlook, linkedin, facebook) where each contact has 1 or more email addresses, write a function to group all contacts which share any email together

Input:

C1: aaa@outlook.com, bbb@gmail.com
​C2: ddd@outlook.com
C3: bbb@gmail.com, aaa@outlook.com, ccc@yahoo.com
C4: ccc@yahoo.com
C5: ddd@outlook.com
C6: eee@outlook.com

I am looking for output :

((C1,C3,C4),(C2,C5),(C6))​

I tried to get all values from hashmap into a hashset and then if the key in hashmap contains the value from set, I add the key to a result arraylist.

The code I am using is :

Set<String> set = new HashSet<>();
        for(Map.Entry<String, LinkedList> entry: hm.entrySet()){
                //System.out.println(entry.getKey()+" "+entry.getValue());  
                set.addAll(entry.getValue());
        }
        ArrayList<String> res1 = new ArrayList<>();
        for(@SuppressWarnings("rawtypes") Map.Entry<String, LinkedList> entry: hm.entrySet()){
            @SuppressWarnings("unchecked")
            Iterator<String> curr = entry.getValue().iterator();
            System.out.println(hm.entrySet());
            while(curr.hasNext()){
                System.out.println("Current= "+curr.next());
                if(set.contains(curr.next())){
                    System.out.println(entry.getKey());
                    res1.add(entry.getKey());
                }
            }
        }   
        System.out.println(set);
        System.out.println(res1);

The output I am getting is :

[C3=[aaa@gmail.com, bbb@outlook.com, ccc@yahoo.com], C4=[ccc@yahoo.com], C5=   
[ddd@outlook.com], C6=[eee@outlook.com], C1=[bbb@outlook.com, 
ddd@outlook.com], C2=[aaa@gmail.com]]

Current= aaa@gmail.com
C3
Current= ccc@yahoo.com
Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList$ListItr.next(LinkedList.java:890)
    at com.logicBuilding.MQ3_HashTable.getCommon(MQ3_HashTable.java:45)
    at com.logicBuilding.MQ3_HashTable.main(MQ3_HashTable.java:7)

Replace this code of yours:

            System.out.println("Current= "+curr.next());
            if(set.contains(curr.next())){

with

            String current = curr.next();
            System.out.println("Current= " + current);
            if(set.contains(current)) {

to avoid calling the next() method without even checking if a next element is available. Also your your if condition wants to check if the string you are currently looking at in is in your set , not the next one already.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM