Map以及Set的遍历(EntrySet方法,补充enumeration和Iterator的区别)

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接口。

时间: 2024-07-30 16:58:02

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

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

http://blog.csdn.net/liu826710/article/details/9001254 在Map集合中 values():方法是获取集合中的所有的值----没有键,没有对应关系, KeySet():将Map中所有的键存入到set集合中.因为set具备迭代器.所有可以迭代方式取出所有的键,再根据get方法.获取每一个键对应的值. keySet():迭代后只能通过get()取key entrySet(): Set<Map.Entry<K,V>> entrySet(

map对象中keyset()和entryset()区别

Set<K> keySet() //返回值是个只存放key值的Set集合(集合中无序存放的) Set<Map.Entry<K,V>> entrySet() //返回映射所包含的映射关系的Set集合(一个关系就是一个键-值对),就是把(key-value)作为一个整体一对一对地存放到Set集合当中的. 下面通过例子看看: 一. keySet()方式. Map<String,String> map = new HashMap<String,String&g

Java中Map的三种遍历方法

Map的三种遍历方法: 1. 使用keySet遍历,while循环: 2. 使用entrySet遍历,while循环: 3. 使用for循环遍历. 告诉您们一个小秘密: (下↓面是测试代码,最爱看代码了,啰嗦再多也没用) 一般人我不告诉他哦. import java.util.*; //0 我的Main界面 public class MapTraverse { public static void main(String[] args) { String[] str = {"I love you

集合框架(一) ----------Map集合遍历的方法

import java.util.*; /** * Map集合遍历的方法 * @author Administrator * */public class Test2 { public static void main(String[] args) { Map<String,String> map=new HashMap<String,String>(); /*Java 提供两种不同的类型: * 引用类型和原始类型(或内置类型). * Int是java的原始数据类型, * Inte

Map的五种遍历方法

package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set; //循环遍历map的方法public class CircleMap { public static void main(String[] args) {  Ma

map的三种遍历方法!

map的三种遍历方法! 集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Templates * and open the template in the editor. */package cn.tsp2c.liubao; import java.util.Collection;import java.util.HashMap;import java.util.Iterator

集合框架Map之entrySet方法的使用

Map的entrySet函数的使用,取得是键和值的映射关系,Entry就是Map接口中的内部接口,类似与我们熟悉的内部类一样,内部类定义在外部类内部,可以直接访问到外部类中的成员 package cn.itcast.map; import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set; public class Map

Java之List和Map的几种遍历方式

/** * list和map遍历 */ import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class list_map遍历{ public static void main(String[] args) { List<String> list=new ArrayList<St

java集合类遍历删除方法测试以及使用场景记录

package test0; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; /**