Map集合小记

来源:互联网 发布:双色球缩水软件免费版 编辑:程序博客网 时间:2024/05/28 06:06
public class MapDemo01 {/** *Map<String, String> map = new HashMap<String, String>(); */public static void main(String[] args) {//创建一个集合Map<String, String> map = new HashMap<String, String>();//向集合内添加数据  以键值对的形式map.put("k1", "小明");map.put("k2", "小红");map.put("k3", "小花");map.put("k4", "小李子");//判断键是否存在if(map.containsKey("k1")){System.out.println("key 存在");}else{System.out.println("key 不存在");}System.out.println("*****************************************************");//判断值是否存在if(map.containsValue("小李")){System.out.println("Value 存在");}else{System.out.println("Value 不存在");}System.out.println("*****************************************************");//定义一个变量接收key为k1的值 打印输出String str = map.get("k1");System.out.println(str);System.out.println("*****************************************************");//输出所有的键Set<String> s = map.keySet();Iterator<String> i = s.iterator();while(i.hasNext()){System.out.println(i.next());}System.out.println("*****************************************************");//输出所有的值Collection<String> c = map.values();Iterator<String> it = c.iterator();while(it.hasNext()){System.out.println(it.next());}}}

原创粉丝点击