zoukankan      html  css  js  c++  java
  • Map集合的两种取出方式

    Map集合有两种取出方式,

    1、keySet:将Map中的键存入Set集合,利用set的迭代器来处理所有的键

    举例代码如下:

    import java.util.*;
    class Test
    {
        public static void main(String[] args)
        {
            Map<String, Integer> map = new HashMap<String, Integer>();
    
            map.put("fan", 23);
            map.put("peng", 45);
            map.put("cheng", 34);
    
            //获取键Set集合
            Set<String> keySet = map.keySet();
            
            Iterator<String> it = keySet.iterator();
    
            while(it.hasNext())
            {
                String keyString = it.next();
                System.out.println(keyString+"-"+map.get(keyString));
    
            }
        }
    }

    2、entrySet

    键Map集合中的键值关系以Set集合的形式返回,然后利用Set的迭代器来使

    形式:Set<Map.Entry<K, V>>

    代码举例如下:

    class Test
    {
        public static void main(String[] args)
        {
            Map<String, String> map = new HashMap<String, String>();
    
            map.put("fan", "fan");
            map.put("peng", "peng");
            map.put("cheng", "cheng");
                    //泛型的嵌套形式,关系是Map.Entry<K, V>类型
            Set<Map.Entry<String, String>> entrySet = map.entrySet();
    
            Iterator<Map.Entry<String, String>> it = entrySet.iterator();
    
            while(it.hasNext())
            {
                Map.Entry<String, String> entry = it.next();
                String key = entry.getKey();
                String value = entry.getValue();
    
                System.out.println(key+"-"+value);
            }
        }
    }
  • 相关阅读:
    mysql 时间戳
    css优先级
    app横竖屏切换
    文本溢出时显示省略号
    react页面间传递参数
    去掉input获取focus时的边框
    Mac中好用的快捷键
    python 图片处理
    CSS padding margin border属性详解
    python3.x执行post请求时报错“POST data should be bytes or an iterable of bytes...”的解决方法
  • 原文地址:https://www.cnblogs.com/fantasy01/p/3975451.html
Copyright © 2011-2022 走看看