一起Talk Android吧(第九十二回:Java中的类集之map三)

本文深入探讨Java中Map接口的entrySet()方法,通过实例讲解如何利用此方法及Map.Entry接口遍历Map集合,对比使用Iterator与foreach的不同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

各位看官们,大家好,上一回中咱们说的是Java中类集之map的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在前面章回中介绍了Map中常用的方法,不过还有一个方法没有详细的介绍,本章回中我们将重点介绍它:entrySet()。这也算是对上一章回的补充吧。

该方法用来返回Map.Entry()类型的Set。通过该方法把Map转换成了Set。有看官说为什么要转换呢?转换后就可以使用Iterator迭代器来输出Set中的元素,进而间接地输出了Map中的元素。为什么不直接使用Iterator输出Map中的元素呢?因为Map中的元素是键值对,包含两个数据,而迭代器只能用来输出一个数据。

Map.Entry()Map中的一个接口,它提供了两个方法用来输出键值对中的键和值:

  • getKey()
  • getValue()

有了这两个方法,再配合迭代器就可以迭代输出Map中键值对。另外,这两个方法也可以配合foreach方法使用,同样可以迭代输出Map中键值对。在这两个输出方法中,我偏向后者。因为使用Iterator进行类型进行转换时比较繁琐,相比之下foreach循环的代码要简洁很多。当然了这是见仁见智的东西,选择一个你自己喜欢的就好。

看官们,接下来我们通过具体的代码来演示如何输出map中的元素。此外,这里的代码包含了前面两个章回中介绍的方法,估计有不少看官一直在等着看这些代码呢。大家可以结合代码回顾一下前面的知识

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapEx {
    static public void main(String args[]) {
        Map<Integer,String> map = new HashMap<>();
        //init the map
        map.put(1, "aaa");
        map.put(2, "bbb");
        map.put(3, "ccc");
        map.put(4, "ddd");
        map.put(5, "eee");
        map.put(6, "fff");

        System.out.println("map: "+map);
        System.out.println("map get: "+map.get(2));

        //find the key of map
        if(map.containsKey(3)) {
            System.out.println("key: 3 is in map.");
        } else {
            System.out.println("key: 3 is not in map.");
        }

        //find the values of map
        if(map.containsValue("abc")) {
            System.out.println("value: abc is in map.");
        } else {
            System.out.println("value: abc is not in map.");
        }

        //add and delete the content of map
        //map.remove(1);
        map.remove(1,"aaa");
        map.put(1, "aaa");
        System.out.println("operation put : "+map.put(1,"abc"));
        System.out.println("operation remove : "+map.remove(9,"aaa"));

        //get the set of all keys from map
        Set<Integer> keySet = map.keySet();
        Iterator<Integer> keyIter = keySet.iterator();
        System.out.print("All  keys  of map: ");
        while(keyIter.hasNext()) {
            System.out.print(keyIter.next()+" ");
        }
        System.out.println("");

        //get the set of all values from map
        Collection<String> valuesSet = map.values();
        Iterator<String> valueIter = valuesSet.iterator();
        System.out.print("All values of map: ");
        while(valueIter.hasNext()) {
            System.out.print(valueIter.next()+" ");
        }
        System.out.println("");

        //output the map by foreach
        System.out.println("show Map as key<--->value : ");
        for(Map.Entry<Integer,String> item: map.entrySet()) {
            System.out.println(item.getKey()+"<--->"+item.getValue());
        }
        System.out.println("");

        //output the map by Iterator 
        System.out.println("show Map as key<--->value : ");
        Set<Map.Entry<Integer, String>> itemSet= map.entrySet();
        // pay attention for the type of Iterator
        Iterator<Map.Entry<Integer, String>> iterItemSet = itemSet.iterator();
        while(iterItemSet.hasNext()){
            Map.Entry<Integer, String> itemOfIterator = iterItemSet.next();
            System.out.println(itemOfIterator.getKey()+"<--->"+itemOfIterator.getValue());
        }
        System.out.println("");
    }
}

下面是程序的运行结果,请参考:

map: {1=aaa, 2=bbb, 3=ccc, 4=ddd, 5=eee, 6=fff}
map get: bbb
key: 3 is in map.
value: abc is not in map.
operation put : aaa
operation remove : false
All  keys  of map: 1 2 3 4 5 6 
All values of map: abc bbb ccc ddd eee fff 
show Map as key<--->value : 
1<--->abc
2<--->bbb
3<--->ccc
4<--->ddd
5<--->eee
6<--->fff

show Map as key<--->value : 
1<--->abc
2<--->bbb
3<--->ccc
4<--->ddd
5<--->eee
6<--->fff

各位看官,关于Java中类集之map的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值