package com.yjf.esupplier.common.test; import java.util.HashMap; import java.util.Map; /** * @author shusheng * @description Map集合的使用 * @Email shusheng@yiji.com * @date 2018/12/17 16:29 */ public class MapDemo1 { public static void main(String[] args) { // 建立集合 Map<String, String> map = new HashMap<String, String>(); //添加元素 System.out.println("map:" + map.put("文章", "馬伊琍")); System.out.println("map:" + map.put("文章", "姚笛")); map.put("鄧超", "孫儷"); map.put("黃曉明", "楊穎"); map.put("周杰倫", "昆凌"); map.put("劉愷威", "楊冪"); System.out.println("map:" + map); //長度 System.out.println("size:" + map.size()); //刪除功能 System.out.println("remove:" + map.remove("黃曉明")); System.out.println("remove:" + map.remove("黃海波")); //判斷功能 System.out.println("containsKey:" + map.containsKey("黃曉明")); System.out.println("containsKey:" + map.containsKey("周杰倫")); System.out.println("containsValue:" + map.containsValue("楊冪")); System.out.println("containsValue:" + map.containsValue("張三")); //刪除功能 map.clear(); System.out.println("isEmpty:" + map.isEmpty()); //輸出集合名稱 System.out.println("map:" + map); } }
打印結果:java
map:null map:馬伊琍 map:{鄧超=孫儷, 文章=姚笛, 周杰倫=昆凌, 黃曉明=楊穎, 劉愷威=楊冪} size:5 remove:楊穎 remove:null containsKey:false containsKey:true containsValue:true containsValue:false isEmpty:true map:{}