Java HashMap key value storage and retrieval

后端 未结 7 1266
眼角桃花
眼角桃花 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:51

    public static void main(String[] args) {
    
        HashMap hashmap = new HashMap();
    
        hashmap.put("one", "1");
        hashmap.put("two", "2");
        hashmap.put("three", "3");
        hashmap.put("four", "4");
        hashmap.put("five", "5");
        hashmap.put("six", "6");
    
        Iterator keyIterator = hashmap.keySet().iterator();
        Iterator valueIterator = hashmap.values().iterator();
    
        while (keyIterator.hasNext()) {
            System.out.println("key: " + keyIterator.next());
        }
    
        while (valueIterator.hasNext()) {
            System.out.println("value: " + valueIterator.next());
        }
    }
    

提交回复
热议问题