map hashmap的使用
Map HashMap 使用
2023-09-27 14:25:11 时间
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模式是用于遍历集合类的标准访问方法。它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。
相关文章
- 报错:Flink cannot assign instance of org.apache.commons.collections.map.LinkedMap to field
- 从头认识java-15.7 Map(6)-介绍HashMap的工作原理-装载因子与性能
- Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)
- 复制或合并map
- 性能指标(模型评估)之mAP与混淆矩阵
- map组件如何展示marker的callout气泡
- Set和Map
- Mybatis Map保存到数据库,Mybatis Map动态同步表,Mybatis Map Foreach插入数据库
- 在JavaScript中获取Map集合中的key和value值(前提是:既不知道key为什么值,也不知道value有哪些值)
- Collection与Map的对比
- map比起unordered_map的优势主要有(hashmap就是unordered_map)
- C++中的STL中map用法详解
- RxJava 中的map与flatMap
- Java小白入门200例110之Java遍历Map集合
- cxf处理一些Map等复杂类型