简体   繁体   中英

How to add elements to a LinkedList that is contained within a HashMap?

I create an adjacency list of vertices using a hashmap where 'V' is the total amount of vertices to be created and 'v' is the individual vertex. 'v' represents both a key for hashmap adj and the name of that vertex. Each key 'v' points to an initially empty LinkedList. I need to access that LinkedList to: 1. add elements to the LinkedList 2. Iterate through the LinkedList 1 element at a time to be implemented within a for-each loop.

HashMap<Integer, List<Integer>>adj = new HashMap<Integer, List<Integer>>();
    for(int v = 0; v < V; v++)
        adj.put(v, new LinkedList<Integer>());

1. Intended function: add int w to end of LinkedList at key 'v'

((LinkedList<Integer>)adj.get(v)).addLast(w);

2. Intended function: for each Node in LinkedList at key 'v', add that Node to key'w' and perform the specified function at each iteration.

for(int w : ((LinkedList<Integer>)adj.get(v).listIterator(0)))
    //DO THIS

Any suggestions for better implementation or possible improvements greatly appreciated.

You can do it like this:

for(Map.Entry<Integer, List<Integer>> entry : adj.entrySet()) {
    for (int w : entry.getValue()) {
        // Do something here
    }
}

Though your question depicts lack of research. But i believe in distributing knowledge, follow the below code snippet that will answer your question.

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 *
 * @author vaibhav.kashyap
 */
public class LinkedListInHashMap {

    private Map<String,LinkedList<String>> myMap = null;
    private LinkedList<String> myLinkedList;
    public static void main(String... wq){
        LinkedListInHashMap lObj = new LinkedListInHashMap();
        lObj.fillMap();
        lObj.displayLinkedListItem();
        lObj.addElement("e");
        lObj.addElement("f");
        lObj.displayLinkedListItem();  
    }

    public void addElement(String str){
        myLinkedList.add(new String(str));
    }
    public void fillMap(){
        myMap = new HashMap<String,LinkedList<String>>();
        myLinkedList  = new LinkedList<String>();
        myLinkedList.add(new String("a"));
        myLinkedList.add(new String("b"));
        myLinkedList.add(new String("c"));
        myLinkedList.add(new String("d"));
        myMap.put("myKey", myLinkedList);
    }
    public void displayLinkedListItem(){
        LinkedList<String> tempList = null;
        for(String key: myMap.keySet()){
            tempList = (LinkedList<String>)myMap.get(key);
            for(String str : tempList){
             System.out.println(str);
            }
        }
        System.out.println("**** End of Result****");
    }
}

output :

a
b
c
d
**** End of Result****
a
b
c
d
e
f
**** End of Result****

& Next time better refer a standard book first & then question. I'll be really happy you run this code and comment dry run of this code. If you'll do that my intent is done. Hope this helps.

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