遍历Map的三种方式

简介: 遍历Map的三种方式

Java遍历Map的三种方式
一个字一个字写出来的,请用心看完,哈哈哈。
1、Set keySet() :
返回所有的key对象的Set集合, 将Map转成Set集合(keySet()),通过Set的迭代器取出Set集合中的每一个元素(Iterator)就是Map集合中的所有的键,再通过get方法获取键对应的值。
实现示例:

import java.util.*;

public class MapTest {

public static void main(String[] args) {
    Map<String,String> map=new HashMap<>();
    map.put("1233r","曹操");
    map.put("12e3","海洋");
    map.put("4378925","咯克");
    map.put("9574","丽人");
    map.put("4325","力士");
    map.put("6543","马瑞");
    System.out.println(map);
    Set<String> set=map.keySet();
    Iterator<String> it=set.iterator();
    while (it.hasNext())
    {
        String key= it.next();
        String value=map.get(key);
        System.out.println("key="+key+"value"+value);
    }
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2、 values() ,获取所有的值,但是Collection values()不能获取到key对象。
实现代码示例:

import java.util.*;

public class MapTest {

public static void main(String[] args) {
    Map<String,String> map=new HashMap<>();
    map.put("1233r","曹操");
    map.put("12e3","海洋");
    map.put("4378925","咯克");
    map.put("9574","丽人");
    map.put("4325","力士");
    map.put("6543","马瑞");
    System.out.println(map);
     Collection<String> values =map.values();
    Iterator<String> it1=values.iterator();
    while(it1.hasNext())
    {
        String a= it1.next();
        System.out.println("value的值为"+a);
    }
    }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
最好一种可以同时获得键值,但需要细心理解。
Map.Entry:
interface Entry<K,V>,通过Map中的entrySet()方法获取存放Map.Entry<K,V>对象的Set集合。
实现代码示例:

import java.util.*;

public class MapTest {

public static void main(String[] args) {
    Map<String,String> map=new HashMap<>();
    map.put("1233r","曹操");
    map.put("12e3","海洋");
    map.put("4378925","咯克");
    map.put("9574","丽人");
    map.put("4325","力士");
    map.put("6543","马瑞");
    System.out.println(map);
         Set<Map.Entry<String,String>> set1=map.entrySet();
    Iterator<Map.Entry<String,String>> it2=set1.iterator();
    while (it2.hasNext())
    {
    Map.Entry<String,String> a1=it2.next();
    String k=a1.getKey();
    String v=a1.getValue();
    System.out.println("key为:"+k+"value为:"+v);

    }
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
蟹蟹!!!

相关文章
Map遍历的几种方法
Map遍历的几种方法
|
3月前
Collection和Map的遍历方式
Collection和Map的遍历方式
20 0
|
1月前
|
JavaScript API
js【最佳实践】遍历数组的八种方法(含数组遍历 API 的对比)for,forEach,for of,map,filter,reduce,every,some
js【最佳实践】遍历数组的八种方法(含数组遍历 API 的对比)for,forEach,for of,map,filter,reduce,every,some
39 1
|
2月前
|
缓存 Java 测试技术
探讨Java中遍历Map集合的最快方式
探讨Java中遍历Map集合的最快方式
27 1
|
2月前
|
存储 缓存 Java
Java遍历Map集合的方法
在Java中,遍历Map集合主要有四种方式:1) 使用`keySet()`遍历keys并用`get()`获取values;2) 使用`entrySet()`直接遍历键值对,效率较高;3) 通过`Iterator`遍历,适合在遍历中删除元素;4) Java 8及以上版本可用`forEach`和Lambda表达式,简洁易读。`entrySet()`通常性能最佳,而遍历方式的选择应考虑代码可读性和数据量。
46 0
Map集合的有序遍历,解决方法多看一下别人的资料
Map集合的有序遍历,解决方法多看一下别人的资料
|
2月前
|
Java API 存储
java中Map遍历详解
java中Map遍历详解
|
3月前
|
Java
|
2月前
遍历Map的四种方法之map.entry详解
遍历Map的四种方法之map.entry详解
|
3月前
|
存储 编译器 Go
Golang深入浅出之-掌握Go语言Map:初始化、增删查改与遍历
【4月更文挑战第21天】Go语言中的`map`提供快速的键值对操作,包括初始化、增删查改和遍历。初始化时,推荐使用`make()`函数,如`make(map[string]int)`。插入和查询键值对直接通过索引访问,更新则重新赋值。删除键值对需用`delete()`函数,确保键存在。遍历map常用`for range`,注意避免在遍历中修改map。了解这些并避免易错点,能提升代码效率和可读性。
44 1
Golang深入浅出之-掌握Go语言Map:初始化、增删查改与遍历