Map集合程序

Map main:

View Code
/*
 * 通过Map转成set(Collection的子接口有iterator有迭代器)就可以迭代。Collection中有迭代器,Map和Collection没有关系,故而没有迭代器
 * 找到了另一个方法。entrySet。
 * 该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型(结婚证)
 * 
 * 取出map中的所有元素。
 *原理,通过keySet方法获取map中所有的键所在的Set集合,在通过Set的迭代器获取到每一个键,
 *在对每一个键通过map集合的get方法获取其对应的值即可。
 */


public class MapDemo {

    public static void main(String[] args) {
        
        Map<Integer,String> map = new HashMap<Integer,String>();
        method_2(map);
    }
    
    public static void method_2(Map<Integer,String> map){
        
        map.put(8,"zhaoliu");
        map.put(2,"zhaoliu");
        map.put(7,"xiaoqiang");
        map.put(6,"wangcai");
        
        Collection<String> values = map.values();
        
        Iterator<String> it2 = values.iterator();
        while(it2.hasNext()){
            System.out.println(it2.next());
        }
        
        
        Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();//两句话合成一句话
        
        while(it.hasNext()){
            Map.Entry<Integer, String> me = it.next();
            Integer key = me.getKey();
            String value = me.getValue();
            System.out.println(key+"::::"+value);
        }
        
        
        Iterator<Integer> it1 = map.keySet().iterator();
        
        while(it1.hasNext()){
            Integer key = it1.next();
            String value = map.get(key);
            System.out.println(key+":"+value);
            
        }
        
    }
    public static void method(Map<Integer,String> map){//学号和姓名
        
        // 添加元素。
        System.out.println(map.put(8, "wangcai"));//null
        System.out.println(map.put(8, "xiaoqiang"));//wangcai 存相同键,值会覆盖。//返回的是
        map.put(2,"zhangsan");
        map.put(7,"zhaoliu");
        
        //删除。
        System.out.println("remove:"+map.remove(2));
        
        //判断。
        System.out.println("containskey:"+map.containsKey(7));
        
        //获取。 
        System.out.println("get:"+map.get(6));
        
        System.out.println(map);//此处排序时无序的,按hashCode
        Outer.Inner.show();
    }
}

interface MyMap{//此处是Map中EntryMap的定义的一个比喻  
    public static interface MyEntry{//内部接口
        void get();
    }
}

class MyDemo implements MyMap.MyEntry{
    public void get(){}
}

class Outer{
    static class Inner{
        static void show(){}
    }
}
原文地址:https://www.cnblogs.com/nauy/p/2817263.html