Java笔记06-Map集合
生活随笔
收集整理的這篇文章主要介紹了
Java笔记06-Map集合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Map集合
學習目標
- 能夠說出Map集合特點
- 使用Map集合添加方法保存數據
- 使用”鍵找值”的方式遍歷Map集合
- 使用”鍵值對”的方式遍歷Map集合
- 能夠使用HashMap存儲自定義鍵值對的數據
- 能夠使用HashMap編寫斗地主洗牌發牌案例
Map集合概述
啥也不用說,Map集合就相當于python中的字典
Java提供了專門的集合類用來存放這種對象關系的對象,即 java.util.Map 接口。
說白了就是鍵值對兒的形式存的數據
Map接口中的常用方法
Map接口中定義了很多方法,常用的如下:
- public V put(K key, V value) : 把指定的鍵與指定的值添加到Map集合中。
- public V remove(Object key) : 把指定的鍵 所對應的鍵值對元素 在Map集合中刪除,返回被刪除元素的
值。 - public V get(Object key) 根據指定的鍵,在Map集合中獲取對應的值。
- public Set keySet() : 獲取Map集合中所有的鍵,存儲到Set集合中。
- public Set<Map.Entry<K,V>> entrySet() : 獲取到Map集合中所有的鍵值對對象的集合(Set集合)。
Map接口的方法演示
public class Demo01 {public static void main(String[] args) { //創建 map對象HashMap<String, String> map = new HashMap<String, String>(); //添加元素到集合map.put("黃曉明", "楊穎");map.put("黃曉明", "楊穎2");map.put("文章", "馬伊琍");map.put("鄧超", "孫儷");System.out.println(map); //String remove(String key)System.out.println(map.remove("鄧超"));System.out.println(map); // 想要查看 黃曉明的媳婦 是誰System.out.println(map.get("黃曉明"));System.out.println(map.get("鄧超"));} }運行結果:
{鄧超=孫儷, 文章=馬伊琍, 黃曉明=楊穎} 孫儷 {文章=馬伊琍, 黃曉明=楊穎} 楊穎 null總結
以上是生活随笔為你收集整理的Java笔记06-Map集合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP从入门到精通 知识体系
- 下一篇: 免越狱版 iOS 抢红包插件