Java HashMap key value storage and retrieval

后端 未结 7 1254
眼角桃花
眼角桃花 2020-12-23 22:27

I want to store values and retrieve them from a Java HashMap.

This is what I have so far:

public void processHashMap()
{
    HashMap hm = new HashMap         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-23 22:50

    Java Hashmap key value example:

    public void processHashMap() {
        //add keys->value pairs to a hashmap:
        HashMap hm = new HashMap();
        hm.put(1, "godric gryfindor");
        hm.put(2, "helga hufflepuff");
        hm.put(3, "rowena ravenclaw");
        hm.put(4, "salazaar slytherin");
    
        //Then get data back out of it:
        LinkedList ll = new LinkedList();
        Iterator itr = hm.keySet().iterator();
        while(itr.hasNext()) {
            String key = itr.next();
            ll.add(key);
        }
    
        System.out.print(ll);  //The key list will be printed.
    }
    

提交回复
热议问题