zoukankan      html  css  js  c++  java
  • 集合框架Map之entrySet方法的使用

     1 Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员
     2 
     3 package cn.itcast.map;
     4 
     5 import java.util.HashMap;
     6 import java.util.Iterator;
     7 import java.util.Map;
     8 import java.util.Map.Entry;
     9 import java.util.Set;
    10 
    11 public class MapEntrySetTest {
    12 
    13  /**
    14   * @param args
    15   */
    16  public static void main(String[] args) {
    17 
    18   Map<Integer, String> map=new HashMap<Integer, String>();
    19   map.put(8, "wangwu");
    20   map.put(2, "lisi");
    21   map.put(7, "zhangsan");
    22   map.put(6, "xuliu");
    23   method_3(map);
    24  }
    25 
    26  private static void method_3(Map<Integer, String> map) {
    27   Set entrySet=map.entrySet();//entrySet()方法返回反应map键值的映射关系,存储在set集合中
    28   Iterator it=entrySet.iterator();//使用迭代器获得每一个映射关系
    29   while(it.hasNext()){
    30    Map.Entry me=(Map.Entry) it.next();//映射关系类型为Map.Entry类型,是一个接口类型
    31    System.out.println(me.getKey()+":::"+me.getValue());
    32 //   System.out.println(me.getValue());
    33   }
    34   
    35  }
    36 
    37 }
  • 相关阅读:
    6-1
    4-9
    4-5
    4-4
    4-3
    3-10
    作业三2
    作业三1
    课堂练习二
    实验三
  • 原文地址:https://www.cnblogs.com/ysw-go/p/5268949.html
Copyright © 2011-2022 走看看