java map中套map

Java Map is part of collections framework. Java Map object is used to store key-value mappings. Java Map can’t contain duplicate keys however duplicate values are allowed.

Java Map是集合框架的一部分。 Java Map对象用于存储键值映射。 Java Map不能包含重复的键,但是允许重复的值。

Java地图 (Java Map)

Some of the important points about Map in java are;

Java中有关Map的一些重要要点是:

  1. Map provides three collection views – set of keys, set of key-value mappings and collection of values.Map提供了三个集合视图–键集,键-值映射集和值集合。
  2. Map doesn’t guarantee order of mappings, however it depends on the implementation. For example, HashMap doesn’t guarantee the order of mappings but TreeMap does.Map不保证映射的顺序,但是取决于实现。 例如,HashMap不能保证映射的顺序,但是TreeMap可以保证。
  3. Map utilise hashCode and equals methods on Key for get and put operations. So mutable classes are not a good fit for Map keys. If the values of hashCode or equals change after put, you won’t get the correct value in get operation.Map利用hashCode和Key上的equals方法进行获取和放置操作。 因此,可变类并不适合用作Map键。 如果放置后hashCode或equals的值发生变化,则get操作中将无法获得正确的值。
  4. Popular implementation classes of Map in Java are HashMap, Hashtable, TreeMap, ConcurrentHashMap and LinkedHashMap.Java中流行的Map实现类是HashMap,Hashtable,TreeMap,ConcurrentHashMap和LinkedHashMap。
  5. AbstractMap class provides skeletal implementation of the Map interface, most of the Map concrete classes extend AbstractMap class and implement required methods.AbstractMap类提供了Map接口的骨架实现,大多数Map具体类扩展了AbstractMap类并实现了所需的方法。

Java Map方法 (Java Map Methods)

Let’s have a look at some of the important Map methods.

让我们看一下一些重要的Map方法。

  1. int size(): returns the number of key-value mappings in this Map.int size() :返回此Map中的键值映射数。
  2. boolean isEmpty(): returns true if there are no mappings present, otherwise false.boolean isEmpty() :如果不存在映射,则返回true,否则返回false。
  3. boolean containsValue(Object value): returns true if there are at least one key mapped to the specified value, otherwise false.boolean containsValue(Object value) :如果至少有一个键映射到指定值,则返回true,否则返回false。
  4. V get(Object key): returns the value mapped to the given key, if no mapping found then returns null.V get(Object key) :返回映射到给定键的值,如果找不到映射,则返回null。
  5. V put(K key, V value): adds the mapping of key-value pair to the map. If there is already a value mapped to this key, then replace the value. This method returns the previous value associated with key, or null if there was no mapping for key.V put(K key,V value) :将键值对的映射添加到映射。 如果已经有一个映射到该键的值,则替换该值。 此方法返回与key关联的先前值;如果没有key映射,则返回null。
  6. V remove(Object key): Removes the mapping for a key from this map if it is present. Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.V remove(Object key) :从此映射中删除键的映射(如果存在)。 返回此映射先前与该键关联的值;如果该映射不包含该键的映射,则返回null。
  7. void putAll(Map<? extends K, ? extends V> m): Copies all of the mappings from the specified map to this map.void putAll(Map <?扩展K,?扩展V> m) :将所有映射从指定映射复制到此映射。
  8. void clear(): removes all the mappings from the Map.void clear() :从地图中删除所有映射。
  9. Set<K> keySet(): returns the Set view of all the keys in the Map. This key set is backed by Map, so any modifications to Map will be reflected to the key set and vice versa.Set <K> keySet() :返回Map中所有键的Set视图。 此密钥集由Map支持,因此对Map的任何修改都会反映到密钥集上,反之亦然。
  10. Collection<V> values(): returns the collection view of all the values in the Map. This collection is backed by Map, so any change in Map will reflect to this values collection and vice versa.Collection <V> values() :返回Map中所有值的集合视图。 此集合由Map支持,因此Map中的任何更改都将反映到该值集合,反之亦然。
  11. Set<Map.Entry<K, V>> entrySet(): returns the Set view of the mappings in the Map. This Set is backed by Map, so any modifications in Map will be reflected in the entry set and vice versa.Set <Map.Entry <K,V >> entrySet() :返回Map中映射的Set视图。 此Set由Map支持,因此Map中的任何修改都将反映在条目集中,反之亦然。

There are few methods in Java Map introduced in Java 8.

Java 8中引入的Java Map中的方法很少。

  1. default V getOrDefault(Object key, V defaultValue): Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.default V getOrDefault(Object key,V defaultValue) :返回指定键所映射到的值;如果此映射不包含该键的映射关系,则返回defaultValue。
  2. default void forEach(BiConsumer<? super K, ? super V> action): Performs the given action for each entry in this map.默认void forEach(BiConsumer <?super K,?super V> action) :对此映射中的每个条目执行给定的操作。
  3. default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function): Replaces each entry’s value with the result of invoking the given function on that entry.默认void replaceAll(BiFunction <?super K,?super V,?extends V> function) :用在该条目上调用给定函数的结果替换每个条目的值。
  4. default V putIfAbsent(K key, V value): If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.默认V putIfAbsent(K键,V值) :如果指定键尚未与值关联(或映射为null),则将其与给定值关联并返回null,否则返回当前值。
  5. default boolean remove(Object key, Object value): Removes the entry for the specified key only if it is currently mapped to the specified value.default boolean remove(Object key,Object value) :仅当当前映射到指定值时,才删除指定键的条目。
  6. default boolean replace(K key, V oldValue, V newValue): Replaces the entry for the specified key only if currently mapped to the specified value.default boolean replace(K key,V oldValue,V newValue) :仅当当前映射到指定值时,才替换指定键的条目。
  7. default V replace(K key, V value): Replaces the entry for the specified key only if it is currently mapped to some value.默认V replace(K键,V值) :仅当当前映射到某个值时,才替换指定键的条目。
  8. default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction): If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.缺省的VulateIfAbsent(K键,Function <?超级K,?扩展V> mappingFunction) :如果指定的键尚未与某个值关联(或映射为null),则尝试使用给定的映射函数和除非为null,否则将其输入此地图。
  9. default V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value. If the function returns null, the mapping is removed.默认VcomputeIfPresent(K键,BiFunction <?super K,?super V,?extended V> remappingFunction) :如果指定键的值存在且不为空,则尝试计算给定键及其当前值的新映射映射值。 如果函数返回null,则删除映射。
  10. default V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction): Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).默认V计算(K键,BiFunction <?super K,?super V,?扩展V> remappingFunction) :尝试计算指定键及其当前映射值的映射(如果没有当前映射,则为null)。
  11. default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction): If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null.默认V merge(K键,V值,BiFunction <?super V,?super V,?extended V> remappingFunction) :如果指定键尚未与值关联或与null关联,则将其与给定的non关联-null值。 否则,用给定的重映射函数的结果替换关联的值,如果结果为null,则将其删除。

You will notice that all the new methods added in the Java 8 Map interface are default methods with implementation. This is done to make sure no compilation error occurs for any classes implementing Map interface.

您会注意到,Java 8 Map接口中添加的所有新方法都是带有实现的默认方法。 这样做是为了确保任何实现Map接口的类都不会发生编译错误。

Java Map示例 (Java Map Example)

Let’s have a look at a simple program for Java Map example. We will use Map implementation class HashMap for our example program.

让我们看一下Java Map示例的简单程序。 我们将在示例程序中使用Map实现类HashMap。

package com.journaldev.examples;import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;public class MapExample {public static void main(String[] args) {Map<String, String> data = new HashMap<>();data.put("A", "A"); // put exampledata.put("B", "B");data.put("C", "C");data.put("D", null); // null valuedata.put(null, "Z"); // null keyString value = data.get("C"); // get exampleSystem.out.println("Key = C, Value = " + value);value = data.getOrDefault("E", "Default Value");System.out.println("Key = E, Value=" + value);boolean keyExists = data.containsKey(null);boolean valueExists = data.containsValue("Z");System.out.println("keyExists= " + keyExists + ", valueExists= " + valueExists);Set<Entry<String, String>> entrySet = data.entrySet();System.out.println(entrySet);System.out.println("data map size=" + data.size());Map<String, String> data1 = new HashMap<>();data1.putAll(data);System.out.println("data1 mappings= " + data1);String nullKeyValue = data1.remove(null);System.out.println("data1 null key value = " + nullKeyValue);System.out.println("data1 after removing null key = " + data1);Set<String> keySet = data.keySet();System.out.println("data map keys = " + keySet);Collection<String> values = data.values();System.out.println("data map values = " + values);data.clear();System.out.println("data map is empty =" + data.isEmpty());}}

Output of above Map example program is;

以上Map示例程序的输出为;

Key = C, Value = C
Key = E, Value=Default Value
keyExists= true, valueExists= true
[null=Z, A=A, B=B, C=C, D=null]
data map size=5
data1 mappings= {null=Z, A=A, B=B, C=C, D=null}
data1 null key value = Z
data1 after removing null key = {A=A, B=B, C=C, D=null}
data map keys = [null, A, B, C, D]
data map values = [Z, A, B, C, null]
data map is empty =true

That’s all for a quick round up on Java Map interface. For Java Map example of new methods introduced in Java 8, please read Java HashMap.

这就是快速浏览Java Map界面的全部内容。 有关Java 8中引入的新方法的Java Map示例,请阅读Java HashMap

翻译自: https://www.journaldev.com/11641/java-map

java map中套map

java map中套map_Java Map – Java中的Map相关推荐

  1. java for循环中map_Java中用for循环取Map

    根据JDK5的新特性,Java中用for循环取Map,例如循环Map的Key view sourceprint? 1 for(String dataKey : paraMap.keySet())   ...

  2. 【史上最强实战项目合集】java项目20套 +完整版java学习视频

    如果你是初学者,或者是自学者!你可以加小编微信(2782278837)!小编可以给你一些好的建议以及给你(免费)提供学习资料!你在学习上有什么问题都可以咨询小编!小编都会为你解答! 注:本公众号纯属个 ...

  3. python中typeerror怎么解决_Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法...

    目前正在学习python,使用的工具为python3.2.3.发现3x版本和2x版本有些差异,在套接字编程时,困扰了我很久,先将python核心编程书中的例子 代码如下: 服务器端: #Echo se ...

  4. java 不可修改的map_Java中如何实现不可变Map详解

    前言 有时最好不允许修改  java.util.Map, 例如跨线程共享只读数据.为此,我们可以使用Unmodifiable Map或Immutable Map. 在这个快速教程中,我们将看到它们之间 ...

  5. java 8 不可变map_Java中如何实现不可变Map详解

    前言 有时最好不允许修改  java.util.map, 例如跨线程共享只读数据.为此,我们可以使用unmodifiable map或immutable map. 在这个快速教程中,我们将看到它们之间 ...

  6. java map套arraylist,在Java中的HashMap和ArrayList的区别?

    In Java, ArrayList and HashMap are used as collections. But I couldn't understand in which situation ...

  7. java 字符串转换成map_java中string类型转换成map

    背景:有时候string类型的数据取出来是个很标准的key.value形式,通过Gson的可以直接转成map 使用方式: Gson gson = new Gson(); Map map = new H ...

  8. Java中的容器类List、Set、Map的对比

    好长时间没有更新博客了,因为这段时间开始学习Java编程思想这本书,希望可以对Java有一个更深入的了解,以便在处理android程序的时候,可以得心应手一些. 今天在看到Java中的容器时,发现了一 ...

  9. java map 随机取值_随机获取一个集合(List, Set)中的元素,随机获取一个Map中的key或value...

    利用Java提供的Random类.从List或Set中随机取出一个元素,从Map中随机获取一个key或value. 因为Set没有提供get(int index)方法,仅仅能先获取一个随机数后.利用一 ...

最新文章

  1. [转]verilog语法学习心得
  2. mysql监控nginx_mysql和nginx服务是否正常监控脚本
  3. 普通平键的主要尺寸有_?办公桌分类及尺寸介绍
  4. 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)--转
  5. step7db块寻址_step7中的难点:间接寻址示例,中文详细注释。
  6. ==和equals()比较
  7. sqlyog如何设置.时提示字段名_Spring boot 中使用 Tomcat时 用户名 密码如何设置呢?...
  8. Qt4_使用SAX读取XML
  9. 李飞飞创建的AI4All启动首次mentorship计划
  10. 2012年4月份第2周51Aspx源码发布详情
  11. 1051: [HAOI2006]受欢迎的牛 (tarjan强连通分量+缩点)
  12. 【车间调度】基于matlab遗传算法求解置换流水车间调度问题【含Matalb源码 176期】
  13. firefox火狐浏览器显示多列书签菜单
  14. python提取实部虚部_Python 复数数据类型详解(complex)[学习 Python 必备基础知识][看此一篇就够了]|python基础教程|python入门|python教程...
  15. W3C官网查找资源教程
  16. mysql 5.1 变量专题
  17. 关于裁判文书网的一些建议
  18. struts中文乱码解决方法详解
  19. mysql学习系列(1)
  20. UML2.0包含的14种图

热门文章

  1. KnockoutJS 3.X API 第四章 表单绑定(11) options绑定
  2. Ubuntu 左边栏和顶栏都不见了,ctrl+alt+t 也调用不出terminal
  3. EF 保证线程内唯一 上下文的创建
  4. document.addEventListener的使用介绍
  5. QT生成在Windows下有图标的exe文件(IDE=QT Creator)
  6. JAVA中的hasNextInt()方法多次调用只有一个结果的原因
  7. [转载] PyTorch简介
  8. [转载] python中numpy库的使用举例
  9. ESP分区重建,解决各种引导问题
  10. RIA and volta