package cn.stat.p8.map.demo; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class MapDemo2 { public static void main(String[] args) { Map<Integer,String> mapset=new HashMap<Integer,String>(); method_2(mapset); } private static void method(Map<Integer, String> mapset) { mapset.put(8,"list"); mapset.put(9,"list6"); mapset.put(3,"list2"); mapset.put(6,"list7"); mapset.put(1,"list9"); Set<Map.Entry<Integer,String>> ks=mapset.entrySet(); Iterator<Map.Entry<Integer,String>> it=ks.iterator(); while(it.hasNext()) { Map.Entry<Integer,String> me=it.next(); Integer key=me.getKey(); String value=me.getValue(); System.out.println(value+" "+key); } } public static void method_2(Map<Integer, String> mapset) { mapset.put(8,"list"); mapset.put(9,"list6"); mapset.put(3,"list2"); mapset.put(6,"list7"); mapset.put(1,"list9"); Collection<String> values=mapset.values(); Iterator<String> it2=values.iterator(); while(it2.hasNext()) { System.out.println(it2.next()); } } }
treemap使用
package cn.stat.p7.map.demo; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class MapDemo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Map<Integer,String> map=new TreeMap<Integer,String>(); method(map); } public static void method(Map<Integer,String> map) { //添加 System.out.println(map.put(8,"wangcai")); System.out.println(map.put(8,"xiaoxiao")); map.put(2, "lili"); map.put(9, "niuniu"); System.out.println(map); //删除 System.out.println(map.remove(2)); System.out.println(map); //判断 System.out.println("containskey:"+map.containsKey(8)); System.out.println("containsvalues:"+map.containsValue("222")); //获取 System.out.println(map.get(8));//不存在返回null } }
时间: 2024-10-09 00:32:57