溫馨提示×

hashmap初始化的方式有哪些

小億
164
2024-03-11 14:55:24
欄目: 編程語言

  1. 使用HashMap類的構造方法創建一個空的HashMap對象:
HashMap<String, Integer> map = new HashMap<>();
  1. 使用HashMap類的構造方法創建一個包含指定初始容量和負載因子的HashMap對象:
HashMap<String, Integer> map = new HashMap<>(16, 0.75f);
  1. 使用HashMap類的put方法逐個添加鍵值對:
HashMap<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
  1. 使用Collections類的靜態方法singletonMap創建一個包含單個鍵值對的不可修改的Map對象:
Map<String, Integer> map = Collections.singletonMap("key", value);

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女