遍历map集合的几种方法

周娟娟 周娟娟     2022-09-08     512

关键词:

遍历Map集合的几种方法

 


 

方法1:使用迭代器iterator遍历集合

HashMap<Integer, Long> map = new HashMap<Integer, Long>();
  for (int i = 1; i <= 50; i++) {
  map.put(i, Math.round(3.14*i*i));
}

// map转换为set集合
Set<Entry<Integer, Long>> set = map.entrySet();

// 使用迭代器Iterator遍历set集合 
Iterator
<Entry<Integer, Long>> it = set.iterator();   
while (it.hasNext()) {   
  Entry
<Integer, Long> next = it.next();   
  Integer key
= next.getKey();   
  Long value
= next.getValue();   
  System.out.println(key
+":"+value);
}

 

方法2:使用增强for循环遍历集合


HashMap<Integer, Long> map = new HashMap<Integer, Long>();
  for (int i = 1; i <= 50; i++) {
  map.put(i, Math.round(3.14*i*i));
}

// map转换为set集合
Set<Entry<Integer, Long>> set = map.entrySet();
for (Entry<Integer, Long> entry : set) {
  Integer key = entry.getKey();
  Long value = entry.getValue(); 
  System.out.println(key+":"+value);
}

 

map集合的几种遍历方式

Map<String,String>map=newHashMap<String,String>();map.put("1","value1");map.put("2","value2");map.put("3","value3");//第一种遍历方式:for(Stringkey:map.keySet()){Stringkey=key;Stringvalue=map.get( 查看详情

java遍历map的几种方法(代码片段)

...结前言    大家平时在使用Java开发时,经常会遇到遍历Map对象的问题。本文就给大家介绍几种Java遍历Map对象的方法,并简单分析一下每种方法的效率。首先创建一个Map对象,初始值为3条数据,用于测试代码的... 查看详情

遍历集合的几种方式

1,使用迭代器Iterator的方式。2, 使用增强for循环的方式。3, 如果有下标,则可以使用下标的方式。(1)遍历数组(2)遍历List集合(3)遍历Set集合(4)遍历Map集合  查看详情

java遍历集合的几种方法(代码片段)

遍历集合的几种方法用不同的方法遍历集合。publicinterfaceIterator:对Collection进行迭代的迭代器。迭代器取代了JavaCollectionsFrameWork中的Enumerationimportjava.util.ArrayList;importjava.util.Collection;importjava.util.Enumeration;importjava.u 查看详情

map的几种遍历方法

private Hashtable<String, String> emails = new Hashtable<String, String>();    //方法一: 用entrySet()    Iterato 查看详情

map遍历的几种方法

复习map的过程中想到的,做个简单的记录1publicclassHashMapTest{23publicstaticvoidmain(Stringargs[]){4Map<Integer,Integer>hm=newHashMap<Integer,Integer>();5hm.put(1,8);6hm.put(2,7);7hm.put(3,6);8hm.put(4,5);9 查看详情

java中遍历map的几种方法

....entrySet();一类是基于map的key;map.keySet()而每一类都有两种遍历方式:a.利用迭代器iterator;b.利用for-each循环;  代码举例如下: [java] viewplaincopy package cn.wzb;import java.u 查看详情

java遍历list中的map的几种方法

 Stueng 类publicclassStudent{ privateStringname; privateintage; privateinttaller; publicStudent(Stringname,intage,inttaller){ this.name=name; this.age=age; this.taller=taller; } @Overrid 查看详情

arraylist集合的几种遍历的方法

ArrayList集合 也可称作动态数组(长度可变),在新建的时候是没有默认长度的,在新增数据长度小于10的时候,ArrayList的长度会自动设置为10 //了解更多可以按住Ctrl再点击你需要查看的方法或者类名, 查看List源代码... 查看详情

遍历循环输出map的几种方式

package常用的遍历方法.forTest;importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjava.util.Map.Entry;publicclassCircleTest publicstaticvoidmain(String[]args) Map<String,Object&g 查看详情

java遍历list,map,vector,set的几种方法

关于list,map,set的区别参考http://www.cnblogs.com/qlqwjy/p/7406573.html1.遍历list@TestpublicvoidtestList(){List<Integer>list=newArrayList<Integer>();list.add(1);list.add(2);System.out.println("----- 查看详情

map遍历的几种方法(代码片段)

importjava.util.HashMap;importjava.util.Iterator;importjava.util.Map;importjava.util.Map.Entry;publicclassMapUtilpublicstaticvoidmain(String[]args)Map<String,Integer>map=newHashMap<String,Integer>();map.put("age",12);map.put("sg",163);map.put(null,null);bl1(map);bl2(map);bl3(map);bl4(map... 查看详情

hashmap的几种遍历方式(代码片段)

一、entrySet 键-值对集合1、iteratorIteratoritem=tempMap.entrySet().iterator();while(item.hasNext())Map.Entryentry=(Map.Entry)item.next();Stringkey=entry.getKey().toString();Integervalue=(Integer)entry.g 查看详情

遍历map的几种方式(代码片段)

Java代码 Map<String,String>map=newHashMap<String,String>();map.put("username","qq");map.put("passWord","123");map.put("userID","1");map.put("email","[email protected]"); 第一种用f 查看详情

迭代dom集合的几种方法

1. Array.prototype.slice.call()   转数组再遍历vara=document.querySelectorAll(‘div‘);vararr=Array.prototype.slice.call(a);console.log(arr); 2.Array.from()将类数组转为数组vara=document.queryS 查看详情

java中便历map的几种方法

常见的Map遍历有下面四种方法:import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;public class MapDemo  public static void main(String[] args)  // 准备好需要遍历的Map HashMap<String, Integer> map = new HashMap<String, Integer&g... 查看详情

map遍历方法

java中遍历MAP的几种方法Java代码Map<String,String>map=newHashMap<String,String>();   map.put("username","qq");   map.put("passWord","123");   map.put("use 查看详情

遍历hashmap的几种方式(代码片段)

1packagecom.test;23importjava.util.HashMap;4importjava.util.Iterator;5importjava.util.Map;67publicclassTest89publicstaticvoidmain(String[]args)throwsException1011Map<String,String>map=newHashM 查看详情