package com.yjf.esupplier.common.test; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author shusheng * @description 根據鍵獲取值,獲取全部的鍵,獲取全部的值 * @Email shusheng@yiji.com * @date 2018/12/17 17:09 */ public class MapDemo2 { public static void main(String[] args) { //建立集合 Map<String, String> map = new HashMap<String, String>(); //添加元素 map.put("鄧超", "孫儷"); map.put("黃曉明", "楊穎"); map.put("周杰倫", "昆凌"); map.put("劉愷威", "楊冪"); map.put("劉愷威", "楊冪1"); //V get(Object key):根據鍵獲取值 System.out.println("get:" + map.get("周杰倫")); System.out.println("-------------------------"); //Set<K> keyset():獲取集合的全部鍵的集合 Set<String> set = map.keySet(); for (String key : set) { System.out.println(key); } System.out.println("-------------------------"); //Collection<V> values():獲取集合中全部值的集合 Collection<String> values = map.values(); for (String v : values) { System.out.println(v); } System.out.println("-------------------------"); Set<Map.Entry<String,String>> entrySet = map.entrySet(); for(Map.Entry<String,String> me:entrySet){ String key = me.getKey(); String value = me.getValue(); System.out.println(key + "---" + value); } } }