public void mearge(Map map) {
Map returnMap = new HashMap<>();
// 转换为Entry
Set<Map.Entry<Object, Object>> entries = map.entrySet();
// 遍历
for (Map.Entry<Object, Object> entry : entries) {
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println("key:value#"+key+":"+val);
}
}

主要使用了Map.entrySet()方法;Entry可以理解为单个的键值对。

这里也跳过了set转为iterator再进行遍历的过程。直接使用foreach的方式,简洁。

补充一个关于Enumeration和iterator的知识点,之前看到有博文指出,尽量少用enumeration,多用iterator。

Enumeration接口主要实现的两个方法:

boolean hasMoreElements()

Tests if this enumeration contains more elements.
E nextElement()

Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

  ·boolean hasMoreElemerts() :测试Enumeration枚举对象中是否还含有元素,如果返回true,则表示还含有至少一个的元素。
      ·Object nextElement() :如果Bnumeration枚举对象还含有元素,该方法得到对象中的下一个元素。

Iterator接口主要方法:

boolean hasNext()

Returns true if the iteration has more elements.
E next()

Returns the next element in the iteration.
void remove()

Removes from the underlying collection the last element returned by this iterator (optional operation).

以上可以看出,iterator比enumeration多了个删除的方法,其他两个方法功能都相似,所以建议多使用iterator接口。

Map以及Set的遍历(EntrySet方法,补充enumeration和Iterator的区别)的更多相关文章

  1. Map<String, String>循环遍历的方法

    Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 Map<String, String>循环遍历的方法 下 ...

  2. LinkedList方法总结 ListIterator和Iterator的区别

    LinkedList也像ArrayList一样实现了基本的接口,但是它执行某些从操作时比ArrayList更高效,但在随机访问方面要逊色一些.LinkedList中有一些方法虽然名字不同,但可以完成相 ...

  3. Map的两张遍历方法 keySet(),entrySet()

    源博客 http://blog.csdn.net/liu826710/article/details/9001254 在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对 ...

  4. Map集合中value()方法与keySet()、entrySet()区别

    http://blog.csdn.net/liu826710/article/details/9001254 在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, ...

  5. Map集合中value()方法与keySet()、entrySet()区别 《转》

    在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet(): 将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键 ...

  6. Map的6种遍历方法

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 探讨有几种遍历Map的方法其实意义并不大,网上的文章一般讲4种或5种的居多,重要的是知道遍历的内涵,从遍历 ...

  7. Java中Map的三种遍历方法

    Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历.   告诉您们一个小秘密: (下↓面是测试代码,最爱看 ...

  8. Map的五种遍历方法

    package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...

  9. map的三种遍历方法!

    map的三种遍历方法!   集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...

随机推荐

  1. mantis邮箱配置

    1.修改/var/www/html/mantisbt-1.3.3/config下config_inc.php配置文件 以163邮箱为例 # --- Email Configuration --- $g ...

  2. css添加样式的四种方式

    1. 导入样式:在 .css文件中使用@import url("...")来引入另一个css样式表 2. 外部样式:在html页面中的head中使用 link 标签引入,如< ...

  3. iOS开发,用了ARC,引入非ARC的第三方,报错

    ARC forbids explicit message sendof'dealloc' 在xcode中,打开“Build Setting”,找到“Objective-C Automatic Refe ...

  4. Designing for iOS: Graphics & Performance

    http://robots.thoughtbot.com/designing-for-ios-graphics-performance  [原文] In the previous article, w ...

  5. css3 视距-perspective

           视距-用来设置用户与元素3d空间Z平面之间的距离. 实例1:       HTML: <div class="perspective"> <h3&g ...

  6. Android中下载、安装和卸载(原)

    应用场景:在检查版本更新的时候经常需要从服务器端下载然后安装到手机中 使用工具: XUtils,这个开源的框架真的是需要花大把时间去阅读和理解的,十分有用的,on the way ! fighting ...

  7. Kafka Topic ISR不全,个别Spark task处理时间长

    现象 Spark streaming读kafka数据做业务处理时,同一个stage的task,有个别task的运行时间比多数task时间都长,造成业务延迟增大. 查看业务对应的topic发现当topi ...

  8. LintCode Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. java IO基础操作

    java IO基础,通熟易懂,好久没复习java 基础了.这里是传送门... http://www.cnblogs.com/nerxious/archive/2012/12/15/2818848.ht ...

  10. Angularjs 服务注册

    $injector: (When you request a service, the $injector is responsible for finding the correct service ...