0
点赞
收藏
分享

微信扫一扫

HashMap 基本使用

前端王祖蓝 04-19 06:00 阅读 22

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

public class DictionaryExample {
    public static void main(String[] args) {
        // 创建HashMap字典
        Map<String, String> dictionary = new HashMap<>();
        
        // 添加键值对
        dictionary.put("apple", "苹果");
        dictionary.put("banana", "香蕉");
        dictionary.put("orange", "橙子");
        
        // 获取值
        String translation = dictionary.get("apple");
        System.out.println("apple 的中文是: " + translation);
        
        // 遍历字典
        for (Map.Entry<String, String> entry : dictionary.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

举报

相关推荐

0 条评论