java操作map集合

java操作map集合

import java.util.HashMap;  
import java.util.Map;  
  
public class MapExample {  
    public static void main(String[] args) {  
        // 创建一个HashMap对象  
        Map<String, Integer> map = new HashMap<>();  
  
        // 添加键值对  
        map.put("apple", 1);  
        map.put("banana", 2);  
        map.put("orange", 3);  
  
        // 查找键  
        Integer key = 2;  
        Integer value = map.get(key);  
        System.out.println(key + " : " + value);  
  
        // 删除键值对  
        map.remove("banana");  
  
        // 遍历集合  
        for (Map.Entry<String, Integer> entry : map.entrySet()) {  
            System.out.println(entry.getKey() + " : " + entry.getValue());  
        }  
    }  
}

 

 

#########################

posted @ 2023-05-03 12:57  西北逍遥  阅读(17)  评论(0编辑  收藏  举报