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);
            }
        }
    }
  • 相关阅读:
    php 克隆和引用类
    php 抽象类、接口和构析方法
    php 面向对象之继承、多态和静态方法
    php封装练习
    php 面向对象之封装
    php 简单操作数据库
    php 练习
    用php输入表格内容
    php 指针遍历、预定义数组和常用函数
    php 数组定义、取值和遍历
  • 原文地址:https://www.cnblogs.com/fantasy01/p/3975451.html
Copyright © 2011-2022 走看看