HashMap遍历之EntrySet

编程入门 行业动态 更新时间:2024-10-27 09:35:01

HashMap<a href=https://www.elefans.com/category/jswz/34/1771029.html style=遍历之EntrySet"/>

HashMap遍历之EntrySet

entry可以获取key和value的具体值

当entry<key,value> 

 public static void main(String[] args) {Map map = new HashMap();map.put("邓超", "孙俪");//替换map.put("王宝强", "马蓉");//okmap.put(null, "刘亦菲");//plmap.put("鹿晗", null);//okmap.put("lu汉", "刘亦菲");map.put("沫年", "刘亦菲");//okSystem.out.println(map);System.out.println("\n==========迭代器===========\n");Iterator ito3 = entrySet.iterator();while(ito3.hasNext()){Map.Entry entry2 = (Map.Entry) ito3.next();System.out.println(entry2.getValue());}//        Iterator ito3 = entrySet.iterator();
//        while(ito3.hasNext()){
//          Object entry = ito3.next();System.out.println(entry);//HashMap$Node ---实现 > Map.Entry (getKey,getValue)
//          //向下转型 Map.Entry
//          Map.Entry m = (Map.Entry) entry;
//            System.out.print(m.getValue()+" ");
//        }}

当entry<key,Object>,此时value = Object  

    public static void main(String[] args) {/*** 使用HashMapp添加三个员工对象,要求* 键:员工id* 值:员工对象** 并且遍历显示工资>18000的员工(遍历方式最少两种)* 员工类:姓名、工资、员工id*/Map map = new HashMap();map.put(1, new Employee("smith", 18000, "20001206"));map.put(2, new Employee("tom", 28000, "20011212"));map.put(3, new Employee("smith", 10000, "20111010"));System.out.println("员工的个数为:" + map.size());System.out.println("=========第三种遍历方式=========");Set set = map.entrySet();//先将map转化为entrySetIterator ito2 = set.iterator();//构建迭代器while (ito2.hasNext()){//迭代器循环条件Map.Entry entry = (Map.Entry) ito2.next();//将entrySet 转化为entry,并且向下移动指针//通过entry来获取key和valueEmployee emp = (Employee) entry.getValue();//将包entry向下转型为EmployeeSystem.out.println(emp);}
}

更多推荐

HashMap遍历之EntrySet

本文发布于:2023-12-05 23:56:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1665803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遍历   HashMap   EntrySet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!