package com.xgx.demo1; import java.util.Collection; import java.util.HashMap; import java.util.Set; public class Map { public static void main(String[] args) { HashMap<String,String> data = new HashMap<>(); data.put("key1","书山有路勤为径"); data.put("key2","苦海无涯苦作舟"); String value = data.get("key1"); System.out.println(value); value = data.get("key2"); System.out.println(value); Set<String> set = data.keySet();//得到key的单值集合 for(String key:set){ System.out.println(key+" : "+data.get(key)); } System.out.println("-----------------"); Collection<String>values = data.values(); for(String myvalue: values){ System.out.println(myvalue); } } }
书山有路勤为径 苦海无涯苦作舟 key1 : 书山有路勤为径 key2 : 苦海无涯苦作舟 ----------------- 书山有路勤为径 苦海无涯苦作舟//结果所示