package com.msb.test10; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * @author : liu * 日期:08:46:56 * 描述:IntelliJ IDEA * 版本:1.0 */ public class Test01 { //这是一个main方法:是程序的入口 public static void main(String[] args) { /* 增加:put(K key, V value) 删除:clear() remove(Object key) 修改: 查看:entrySet() get(Object key) keySet() size() values() 判断:containsKey(Object key) containsValue(Object value) equals(Object o) isEmpty() * */ //创建一个Map集合:无序,唯一 Map<String,Integer> map=new HashMap<>(); System.out.println(map.put("lili", 1010101010)); map.put("nana",1234523410); map.put("feifei",345678941); System.out.println(map.put("lili", 345601010)); map.put("mingming",1010101010); //map.clear();//清空 // /*map.remove("feifeji");移除*/ System.out.println(map.size()); System.out.println(map); //System.out.println(map.containsKey("lili")); //System.out.println(map.containsValue(3456010)); Map<String,Integer> map2=new HashMap<>(); System.out.println(map2.put("lili", 1010101010)); map2.put("nana",1234523410); map2.put("feifei",345678941); System.out.println(map2.put("lili", 345601010)); map2.put("mingming",1010101010); System.out.println(map == map2); System.out.println(map.equals(map2));//equals进行了重写,比较的是具体的值 System.out.println(map.isEmpty());//判断是否为空 System.out.println(map.get("nana")); System.out.println("========================="); //keySet()对集合中key进行遍历查看 Set<String> set = map.keySet(); for (String s:set ) { System.out.println(s); } System.out.println("========================"); //values()对合计中的value进行遍历查看 Collection<Integer> values = map.values(); for (Integer i:values ) { System.out.println(i); } System.out.println("========================"); //get(object key) keySet() Set<String> set1 = map.keySet(); for (String s:set1 ) { System.out.println(map.get(s)); } System.out.println("========================"); //entrySet Set<Map.Entry<String, Integer>> entries = map.entrySet(); for (Map.Entry<String, Integer> e:entries ) { System.out.println(e.getKey()+"-----"+ e.getValue()); } } }
标签:Map,常用,map,System,接口,map2,put,println,out From: https://www.cnblogs.com/jeldp/p/16862517.html