遍历map集合的几种方式

每天进步一点点,好记性不如烂笔头 每天进步一点点,好记性不如烂笔头     2022-09-02     186

关键词:

 1 import java.util.HashMap;
 2 import java.util.Iterator;
 3 import java.util.Map;
 4 import java.util.Map.Entry;
 5 
 6 /**
 7  * <p>遍历Map集合</p>
 8  * @author:[email protected]
 9  * @date:2017-5-30
10  */
11 public class Test {
12     public static void main(String[] args) {
13         Map<String, String> map = new HashMap<String, String>();
14         map.put("username", "yujiwei");
15         map.put("password", "12345");
16         map.put("address", "hangzhou");
17         map.put("love", "编程");
18         //1.获取所有的key
19         for(String key : map.keySet()){//返回的是map的key值
20             String value = map.get(key);//通过key取value
21             System.out.println("key = " + key + ",value = " + value);
22         }
23         
24         System.out.println("----------------------------------");
25         
26         //2.通过map.entrySet的iterator来遍历Map集合
27         Iterator<Entry<String, String>> it = map.entrySet().iterator();
28         while(it.hasNext()){
29             Entry<String, String> entry = it.next();
30             System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
31         }
32         
33         System.out.println("----------------------------------");
34         
35         //3.通过Map.Entry来遍历Map集合
36         for(Map.Entry<String, String> entry : map.entrySet()){
37             System.out.println("key= " + entry.getKey() + " and value= "+ entry.getValue());
38         }
39     }
40 }

 

遍历集合的几种方式

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

遍历map集合的几种方法

遍历Map集合的几种方法  方法1:使用迭代器iterator遍历集合HashMap<Integer,Long>map=newHashMap<Integer,Long>();  for(inti=1;i<=50;i++){  map.put(i,Math.round(3.14*i*i));}//map转换为set集合Set<Entry< 查看详情

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的几种方式

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

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

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

map遍历的几种方式和效率问题

一、map遍历的效率先创建一个map,添加好数据:Map<String,String>map=newHashMap<>();for(inti=0;i<1000000;i++)map.put(i+"",i+"AA");1、keySet的for循环方式://只获取keypublicstaticvoidkeySetForGetKey(Map<String,String>map)longstartTime=System.currentTimeMi... 查看详情

遍历map集合的集中方式

...合中没有重复的键对象,值对象可以重复。(Key-Value);遍历map集合的方式有几种,下面介绍几种:importjava.util.HashMap;importjava.util.Iterator;importjava.util.MappublicclassMapTest{  publicstaticv 查看详情

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

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

java中遍历map的几种方法

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

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

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

js循环遍历变量的几种方式

参考技术Ajs循环遍历变量的方式有以下几种:1.for(leti=0;i<5;i++)2.forEach3.forof4.forin那么我们来看下这几种遍历方式的用法,以及退出循环的方法1.for这是最常用的遍历方法,for用来遍历数组,可以使用break退出循环,使用continue来... 查看详情

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遍历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源代码... 查看详情

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("----- 查看详情

java8遍历数组的几种方式

参考技术Alist集合的遍历3种方法:[java]viewplaincopypackagecom.sort;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.List;/***list的三种遍历*@authorOwner**/publicclassListTestpublicstaticvoidmain(String[]args)List<String>list=newArrayList<String>();list... 查看详情