Java—Map.Entry

 

Map是java中的接口,Map.Entry是Map的一个内部接口。

Map提供了一些常用方法,如keySet()、entrySet()等方法。

keySet()方法返回值是Map中key值的集合;entrySet()的返回值也是返回一个Set集合,此集合的类型为Map.Entry。

Map.Entry是Map声明的一个内部接口,此接口为泛型,定义为Entry<K,V>。它表示Map中的一个实体(一个key-value对)。接口中有getKey(),getValue方法。

 

 

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map.Entry; 

import java.util.Map; 

 

public class hello {

public static void main(String[] args){

hashMapTest();

 

}

 

public static void hashMapTest() {

System.out.println("----------------hashMapTest---------------");

 

Map<String,String> map1 = new HashMap<String,String>(); 

map1.put("1", "A");

map1.put("2", "B");

map1.put("3", "C");

map1.put("4", "D");

map1.put("5", "E");

map1.put("6", "F");

 

Iterator<Entry<String, String>> it = map1.entrySet().iterator();

while(it.hasNext()){

Map.Entry<String,String> e = (Map.Entry<String,String>) it.next();

System.out.println(e.getKey() + " : " + e.getValue()); 

}

 

System.out.println(map1);

}

}

 

posted @ 2015-01-06 17:28  yuyue2014  阅读(228)  评论(0编辑  收藏  举报