• Map.Entry


    package MapTest;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class EntryTest {
    
    
        public static void main(String[] args){
            HashMap<String, String> map = new HashMap<>();
            map.put("1","kangkang");
            map.put("2","xiaowang");
            //静态方法调用非静态方法--》实例化一个对象--》通过对象调用里面的方法
            EntryTest entryTest = new EntryTest();
    //        entryTest.MapEntry(map);
            entryTest.entrySetTest(map);
                }
        //map.entry
        public  void MapEntry(Map map){
    
            //获取map的值
            //获取k-v对集合
            Set<Map.Entry<String, String>> entries = map.entrySet();
            if(entries != null) {
                //得到迭代器
                Iterator<Map.Entry<String, String>> iterator = entries.iterator();
                //开始迭代
                while (iterator.hasNext()) {
                    //得到一个Map.Entry实例化的对象
                    Map.Entry<String, String> student = iterator.next();
                    //获取key和value
                    String id = student.getKey();
                    String name = student.getValue();
                    System.out.println(id + "==" + name);
                }
            }
        }
        //keyset
        public void entrySetTest(Map map){
            //获取key集合,存储的是所有的id
            Set set = map.keySet();
            if(set != null){
                Iterator iterator = set.iterator();
                while (iterator.hasNext()){
                    Object id = iterator.next();
                    //这里需要重复调用map.get()方法
                    Object name = map.get(id);
                    System.out.println(id + "==" + name);
    
                }
    
            }
    
        }
    
    }
    

    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    点分治 (等级排) codeforces 321C
    树上点分治 poj 1741
    判断点在直线的左侧还是右侧
    树的重心
    链式前向星
    树上点的分治
    构造 素数
    二进制 + 模拟
    枚举 + 三分 (游标)
    枚举 + 三分
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326902.html
Copyright © 2020-2023  润新知