package map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; /**
* Map的实现类HashMap使用
*/
public class MapTest {
/**
*
*/
public static void main(String[] args) {
System.out.println("MapGame start...");
BasicUseOfHashMap();
System.out.println("MapGame end...");
} /**
* HashMap的使用
*/
private static void BasicUseOfHashMap() {
Map<String, String> hashmap = new HashMap<>();
hashmap.put("name", "eric");
hashmap.put("sex", "男");
String value = hashmap.get("sex");
System.out.println(value);
/**
* 增强for循环遍历之使用entrySet循环遍历
*/
System.out.println("\r\n" + "使用entrySet循环遍历");
for (Map.Entry<String, String> entry : hashmap.entrySet()) {
String key1 = entry.getKey();
String value1 = entry.getValue();
System.out.println(key1 + ":" + value1);
}
/**
* 增强for循环遍历之使用keySet循环遍历
*/
System.out.println("\r\n" + "使用keySet循环遍历");
for (String key2 : hashmap.keySet()) {
System.out.println(key2 + ":" + hashmap.get(key2));
}
/**
* 迭代器循环遍历之使用keySet()遍历
*/
System.out.println("\r\n" + "迭代器循环遍历之使用keySet()遍历");
Iterator<String> iterator = hashmap.keySet().iterator();
while (iterator.hasNext()) {
String key3 = iterator.next();
System.out.println(key3 + ":" + hashmap.get(key3));
}
/**
* 迭代器循环遍历之使用entrySet()遍历
*/
System.out.println("\r\n" + "迭代器循环遍历之使用keySet()遍历");
Iterator<Map.Entry<String, String>> iterator1 = hashmap.entrySet().iterator();
while (iterator1.hasNext()) {
Map.Entry<String, String> map = iterator1.next();
String key4 = map.getKey();
String value4 = map.getValue();
System.out.println(key4 + ":" + value4);
}
} }

   java中为什么要使用Iterator?

Iterator模式是用于遍历集合类的标准访问方法。它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。

参考资料:https://www.cnblogs.com/lzq198754/p/5780165.html#top

map hashmap的使用的更多相关文章

  1. Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |

    Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...

  2. ES6 & Map & hashMap

    ES6 & Map & hashMap 01 two-sum https://leetcode.com/submissions/detail/141732589/ hashMap ht ...

  3. Map HashMap 排序 迭代循环 修改值

    HashMap dgzhMap = Dict.getDict("dgzh"); Iterator it_d = dgzhMap.entrySet().iterator(); whi ...

  4. Map随笔:最常用的Map——HashMap

    目录 Map随笔:最常用的Map--HashMap 前言: 1,HashMap的结构 2,HashMap的一些属性(JDK8) 3,HashMap的构造函数(JDK8) 4,HashMap的一些方法( ...

  5. [Java] Map / HashMap - 源代码学习笔记

    Map 1. 用于关联 key 和 value 的对象,其中 key 与 key 之间不能重复. 2. 是一个接口,用来代替 Java 早期版本中的 Dictionary 抽象类. 3. 提供三种不同 ...

  6. 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap

    平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...

  7. Map / HashMap 获取Key值的方法

    方法1:keySet()HashMap hashmp = ne HashMap();hashmp.put("aa", "111");Set set = hash ...

  8. Map:HashMap和TreeMap

    一.Map集合     特点:将键映射到值得对象 Map集合和Collection集合的区别? Collection:是单列集合,存储的是单独出现的元素    Map: 是双列集合,存储的是键值对形式 ...

  9. Java集合 之Map(HashMap、Hashtable 、TreeMap、WeakHashMap )理解(new)

    HashMap 说明: 在详细介绍HashMap的代码之前,我们需要了解:HashMap就是一个散列表,它是通过“拉链法”解决哈希冲突的.还需要再补充说明的一点是影响HashMap性能的有两个参数:初 ...

  10. golang 多维哈希(map,hashmap)实践随笔

    有些场景使用多维哈希来存储数据,时间复杂度恒定,简单粗暴好用.这里记录一下. 如下是三维哈希的简单示意图,建议层数不要太多,否则时间久了,自己写的代码都不认识. 下图是三维哈希在内存的存储形式,has ...

随机推荐

  1. Vue-cli 项目设置每个页面标题

    页面标题 在vue-router页面配置中添加meta的title信息,配合vue-router的beforeEach注册一个前置守卫用户获取到页面配置的title const title = '移动 ...

  2. ubuntu软件源变更阿里源和arm板子变更国内源

    使用 lsb_release -c 命令,看看当前版本号,替换一下“bionic”字符 1.更改 /etc/apt/sources.list,更改之前先备份一下 deb http://mirrors. ...

  3. springboot 加载jsp 刷新jsp ,刷新Controller (亲自尝试)

    解决jsp加载成功.<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId&g ...

  4. git常用操作命令1

    1. 本地库初始化操作 命令: git init 效果: Initialized empty Git repository in E:/ws/git/ws/.git/ 会在当前目录(E:/ws/git ...

  5. spring-cloud:Hystrix熔断的使用示例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springCl ...

  6. Activity 启动模式总结

    Activity 启动模式: 1. standard: 默认启动模式,每次启动一个Activity都会重新创建一个实例: 2. singleTop: 栈顶复用模式,新Activity位于任务栈的栈顶, ...

  7. [CSP-S模拟测试]:Star Way To Heaven(最小生成树Prim)

    题目描述 小$w$伤心的走上了$Star\ way\ to\ heaven$. 到天堂的道路是一个笛卡尔坐标系上一个$n\times m$的长方形通道(顶点在$(0,0)$和$(n,m)$),小$w$ ...

  8. 【转】C#反编译工具

    源地址:https://blog.csdn.net/kongwei521/article/details/54927689 源地址:https://www.cnblogs.com/JamesLi201 ...

  9. GIT安装包备用地址

    如果官网下载被禁止,可在下面这个地址下载,速度飞快 http://www.wmzhe.com/soft-38801.html#download

  10. python中的_ElementUnicodeResult是什么

    _ElementUnicodeResult在python中是字符串的一种,因为在python3中,字符串就是指以unicode编码规则存储的数据,而以其他方式如utf-8,ASCII编码方式存储的数据 ...