Map对象中的keyset()、entryset()和Map.Entry

一 Map对象中的keySet()和entrySet()

1. keySet()

public static void main(String[] args) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("01", "qwe");
    map.put("02", "asd");
    map.put("03", "zxc");
    // 先获取map集合的所有键的set集合,即为map中所有key值得集合
    Set<String> keySet = map.keySet();
    // 有了set集合,就可以获取其迭代器
    Iterator<String> it = keySet.iterator();
    while (it.hasNext()) {
        String key = it.next();
        // 有了键可以通过map集合的get方法获取其对应的值
        String value = map.get(key);
        // 获得key和value值
        System.out.println("key:" + key + "-->value:" + value);
    }
}

 

keySet()返回的是map对象的key值的set集合

2. entrySet()

public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("01", "qwe");
        map.put("02", "asd");
        map.put("03", "zxc");
        // 通过entrySet()方法将map集合中的映射关系取出(这个关系就是Map.Entry类型)
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        // 将关系集合entryset进行迭代,存放到迭代器中
        Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();
        while (it2.hasNext()) {
            // 获取Map.Entry关系对象me
            Map.Entry<String, String> me = it2.next();
            // 通过关系对像获取key
            String key2 = me.getKey();
            // 通过关系对像获取value
            String value2 = me.getValue();
            System.out.println("key:" + key2 + "-->value:" + value2);
        }
    }

 

entrySet()返回映射所包含的映射关系的Set集合(一个关系就是一个键-值对),就是把(key-value)作为一个整体一对一对地存放到Set集合当中的。

3.总结

虽然使用keyset及entryset来进行遍历能取得相同的结果,但两者的遍历速度是有差别的。
               keySet():迭代后只能通过get()取key;再根据key值取value。
               entrySet():迭代后可以e.getKey(),e.getValue()取key和value。

同时,keySet()的速度比entrySet()慢了很多,也就是keySet方式遍历Map的性能不如entrySet性能好
为了提高性能,以后多考虑用entrySet()方式来进行遍历。

 

二 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方法。

遍历Map对象的常用方法除了以上两种外,还有一种是单纯的遍历value值。Map有一个values方法,返回的是value的Collection集合。通过遍历Collection也可以遍历value。

public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("01", "qwe");
        map.put("02", "asd");
        map.put("03", "zxc");
        // 创建一个Collection集合,存放map的value值
        Collection<String> c = map.values();
        // 通过遍历Collection也可以遍历value
        Iterator<String> it = c.iterator();
        // 该方法只能遍历value值,不能遍历key值
        while (it.hasNext()) {
            Object value = it.next();
            System.out.println("value:" + value);
        }
    }

 

在遍历Map对象时,先从Map对象中取出key值之后,还必须每次重复返回到Map中取得相对的值,这是很繁琐和费时的。

幸运的是,Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集。 接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法。

Set entries = map.entrySet( );
    if(entries != null) {
    Iterator iterator = entries.iterator( );
    while(iterator.hasNext( )) {
        Map.Entry entry =iterator.next( );
        Object key = entry.getKey( );
        Object value = entry.getValue();
        //
}
}

尽管增加了一行代码,我们却省略了许多对Map不必要的“get”调用。同时,提供给开发人员一个同时保持了关键字和其对应的值的类。Map.Entry同时也提供了一个setValue()方法,程序员可以使用它修改map里面的值。

posted on 2020-08-21 11:22  Code2020  阅读(200)  评论(0编辑  收藏  举报