发表简要目录: (Post Brief Table of Content:)

  • Introduction介绍
  • Java SE 8: Immutable Empty MapJava SE 8:不可变的空映射
  • Java SE 9: Immutable Empty Map With Map.of()Java SE 9:具有Map.of()的不可变空映射
  • Java SE 8: Immutable Non-Empty MapJava SE 8:不可变的非空映射
  • Java SE 9: Immutable Non-Empty Map With Map.of()Java SE 9:具有Map.of()的不可变非空映射
  • Java SE 9: Immutable Map.ofEntries() UtilityJava SE 9:不可变的Map.ofEntries()实用程序
  • Java SE 9: Immutable Map.entry() UtilityJava SE 9:不可变的Map.entry()实用程序

介绍 (Introduction)

Oracle Corporation is going to release Java New Version: Java SE 9 around March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It is my seventh post in this series.

Oracle Corporation将于2017年3月左右发布Java新版本:Java SE9。因此,我想发表一系列有关Java SE 9新功能的文章。 这是本系列的第七篇。

I have already delivered couple of posts on Java SE 9 New Features. Before going through this post, please read them. It is the continuation of my previous two posts:

我已经发表了有关Java SE 9新功能的几篇文章。 在阅读这篇文章之前,请阅读它们。 这是我前两个帖子的延续:

  • Java SE 9: Factory Methods for Immutable List.Java SE 9:不可变列表的工厂方法 。
  • Java SE 9: Factory Methods for Immutable Set.Java SE 9:不可变集的工厂方法 。

In this post, we are going to discuss one more Java SE 9 New Feature: “Factory Methods for Immutable Map and Map.Entry” with some simple and suitable examples.

在本文中,我们将讨论一些Java SE 9新功能: 带有不可变Map和Map.Entry的工厂方法” ,并提供一些简单而合适的示例。

NOTE:-
All Java SE 9 factory methods for Immutable List, Set, and Map are static utility methods.

注意:-
不可变列表,集合和映射的所有Java SE 9工厂方法都是静态实用程序方法。

Java SE 8:不可变的空映射 (Java SE 8: Immutable Empty Map)

Now we will see how to create Empty Immutable Map in Java SE 8 and earlier versions. If we want to create an empty Immutable or Unmodifiable Map, we should use Collections class utility method: unmodifiableMap as shown below:

现在,我们将看到如何在Java SE 8和更早版本中创建Empty Immutable Map。 如果要创建一个空的不可变或不可修改的地图,则应使用Collections类实用程序方法: unmodifiableMap ,如下所示:

Empty Map Example:-

空地图示例:-

Map<Integer,String> emptyMap = new HashMap<>();Map<Integer,String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap);

Test this code with Java SE 9 REPL:

使用Java SE 9 REPL测试此代码:

jshell> Map<Integer,String> emptyMap = new HashMap<>()
emptyMap ==> {}jshell> Map<Integer,String> immutableEmptyMap = Collections.unmodifiableMap(emptyMap)
immutableEmptyMap ==> {}

Here we can observe that to create an empty Immutable Map, we need to do lot of stuff and very tedious and verbose steps. Let us see the same thing in Java SE 9 now.

在这里,我们可以观察到要创建一个空的不可变映射,我们需要做很多事情以及非常繁琐和冗长的步骤。 现在让我们在Java SE 9中看到同样的事情。

Java SE 9:具有Map.of()的不可变空映射 (Java SE 9: Immutable Empty Map With Map.of())

To overcome those shortcomings, Java SE 9 has introduced two sets of useful overloaded methods.

为了克服这些缺点,Java SE 9引入了两组有用的重载方法。

  • Map.of()Map.of()
  • Map.ofEntries()Map.ofEntries()

In this section, we will discuss first set of methods that is Map.of() utility methods and will pick-up next set of methods in the next section.

在本节中,我们将讨论第一套方法Map.of()实用程序方法,并在下一节中介绍下一套方法。

If we go through the Java SE 9 Map API, we can find the below utility methods in Map interface.
Empty Map API Utility

如果通过Java SE 9 Map API,我们可以在Map界面中找到以下实用程序方法。
空Map API实用程序

static <K,V> Map<K,V> of()

This utility method is used to create an empty Immutable Map that is Map with zero element in Java SE 9.

此实用程序方法用于创建一个空的不可变映射,该映射是Java SE 9中零元素的映射。

Empty Map Example:-

空地图示例:-

jshell> Map<Integer,String> emptyImmutableMap = Map.of()
emptyImmutableMap ==> {}

Here we can observe that creating an empty Immutable Map in Java SE 9 is very easy and simple process.

在这里我们可以看到,在Java SE 9中创建一个空的不可变映射是非常简单的过程。

Java SE 8:不可变的非空映射 (Java SE 8: Immutable Non-Empty Map)

In this section, we will see how to create a non-empty Immutable Map in Java SE 8 and Earlier versions.

在本节中,我们将看到如何在Java SE 8和早期版本中创建非空的不可变映射。

We can use same Collections.unmodifiableMap utility method to create non-empty Immutable Map as shown below.

我们可以使用相同的Collections.unmodifiableMap实用程序方法来创建非空的不可变地图,如下所示。

Non-empty Map Example:-

非空地图示例:-

Map<Integer,String> nonemptyMap = new HashMap<>();nonemptyMap.put(1,"one")nonemptyMap.put(2,"two")nonemptyMap.put(3,"three")Map<Integer,String> immutableNonEmptyMap = Collections.unmodifiableMap(nonemptyMap);

Here also we can observe that to create a non-empty Immutable Map, we need to do lot of stuff and very tedious and verbose steps. Let us see the same thing in Java SE 9 now.

在这里,我们还可以观察到,要创建一个非空的不可变映射,我们需要做很多事情以及非常繁琐和冗长的步骤。 现在让我们在Java SE 9中看到同样的事情。

Test this with Java SE 9 REPL:-

使用Java SE 9 REPL进行测试:

jshell> Map<Integer,String> nonemptyMap = new HashMap<>()
nonemptyMap ==> {}jshell> nonemptyMap.put(1,"one")
$2 ==> nulljshell> nonemptyMap.put(2,"two")
$3 ==> nulljshell> nonemptyMap.put(3,"three")
$4 ==> nullonemptyMap)<Integer,String> immutableNonEmptyMap = Collections.unmodifiableMap(n
immutableNonEmptyMap ==> {1=one, 2=two, 3=three}jshell> nonemptyMap
nonemptyMap ==> {1=one, 2=two, 3=three}

Java SE 9:具有Map.of()的不可变非空映射 (Java SE 9: Immutable Non-Empty Map With Map.of())

In this section, we will discuss about How to use Java SE 9 of() overloaded methods to create Immutable Non-Empty Map. If we go through the Java SE 9 API documentation, we will see the following utility methods in Map interface.

在本节中,我们将讨论如何使用Java SE 9 of()重载方法来创建不可变的非空映射。 如果我们浏览Java SE 9 API文档,我们将在Map界面中看到以下实用程序方法。

Non-Empty Map API Utility

非空Map API实用程序

static <K,V> Map<K,V> of(K k1, V v1)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)static <K,V> Map<K,V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)

These useful methods are used to create a new Non-Empty Immutable Map with one element to 10 elements.

这些有用的方法用于创建一个新的非空不可变映射,该映射具有一个元素到10个元素。

Non-Empty Map API Utility

非空Map API实用程序

static <K,V> Map<K,V> of(K k1, V v1)

Non-Empty Map Example:-

非空地图示例:-

jshell> Map<Integer,String> nonemptyImmutableMap = Map.of(1, "one", 2, "two", 3, "three")
nonemptyImmutableMap ==> {2=two, 3=three, 1=one}

Here we can observe that it is very easy to create an Empty and Non-empty Immutable Maps in Java SE 9.

在这里我们可以看到,在Java SE 9中创建一个空的和非空的不可变映射非常容易。

Java SE 9:不可变的Map.ofEntries()实用程序 (Java SE 9: Immutable Map.ofEntries() Utility)

As we know, Java SE 9 has introduced two sets of Map Utility methods to create Immutable Map: of and ofEntries. We have already discussed about Map.of() methods in the previous sections. In this section, we will discuss about “How to use Map.ofEntries() methods” to create empty or non-empty Maps.

众所周知,Java SE 9引入了两组Map Utility方法来创建不可变的Map:of和ofEntries。 在前面的部分中,我们已经讨论过Map.of()方法。 在本节中,我们将讨论“如何使用Map.ofEntries()方法”来创建空或非空Maps。

Map.ofEntries() methods are used to create Immutable or Unmodifiable Maps using Entries.

Map.ofEntries()方法用于使用Entries创建不可变或不可修改的地图。

If we go through the Java SE 9 Map API, we can find the below utility methods in Map interface.
Empty Map API Utility

如果通过Java SE 9 Map API,我们可以在Map界面中找到以下实用程序方法。
空Map API实用程序

static <K,V> Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries)

This var-args method is useful to create an Immutable Map with zero or one or more elements using Entries. We should use Map.entry() utility method to create Entries. Please refer next section to understand this Map.entry() method.

此var-args方法对于使用Entries创建具有零个或一个或多个元素的不可变映射很有用。 我们应该使用Map.entry()实用程序方法创建条目。 请参阅下一节以了解此Map.entry()方法。

Empty Map Example:-

空地图示例:-

jshell> Map<Integer,String> emptyImmutableMap = Map.ofEntries()
emptyImmutableMap ==> {}

This useful method is used to create a new Immutable Map with zero element using Entries.

此有用的方法用于使用Entries创建一个零元素的新不可变地图。

Non-Empty Map Example:-

非空地图示例:-

import static java.util.Map.entry
jshell> Map<Integer,String> emptyImmutableMap = Map.ofEntries(entry(1,"one"),...> entry(2,"two"), entry(3,"three"))
emptyImmutableMap ==> {1=one, 2=two, 3=three}

This useful method is used to create a new Immutable Map with one or more elements using Entries.

这种有用的方法用于使用条目使用一个或多个元素创建新的不可变映射。

Java SE 9:不可变的Map.entry()实用程序 (Java SE 9: Immutable Map.entry() Utility)

As we discussed in the previous section, we can use this Map.entry() Utility method to create an Immutable Map.Entry using given Key and value pairs. It is used as part of Map.ofEntries() method to create an Immutable Map as shown in the above example.

如前一节所述,我们可以使用此Map.entry()实用程序方法使用给定的键和值对创建不可变的Map.Entry。 如上例所示,它用作Map.ofEntries()方法的一部分来创建不可变地图。

Map.entry() method API:-

Map.entry()方法API:-

static <K,V> Map.Entry<K,V> entry(K k, V v)

Map.entry() method Example:-

Map.entry()方法示例:-

jshell> Map.Entry<Integer,String> immutableMapEntry1 = Map.entry(1,"one")
immutableMapEntry1 ==> 1=onejshell> Map.Entry<Integer,String> immutableMapEntry2 = Map.entry(2,"two")
immutableMapEntry2 ==> 2=twojshell> Map.Entry<Integer,String> immutableMapEntry3 = Map.entry(3,"three")
immutableMapEntry3 ==> 3=three

Here we have created three Immutable Map.Entry objects. Using these Entry objects, we can create an Immutable Map as shown below:

在这里,我们创建了三个Immutable Map.Entry对象。 使用这些Entry对象,我们可以创建一个不可变映射,如下所示:

jshell> Map<Integer,String> immutableMap = Map.ofEntries(immutableMapEntry1,...> immutableMapEntry2, immutableMapEntry3)
immutableMap ==> {3=three, 2=two, 1=one}

NOTE:-
Characteristics of a Immutable Map are similar to Immutable List. You can find those information in detail here: Java SE 9: Factory Methods for Immutable List.

注意:-
不可变映射的特征类似于不可变列表。 您可以在这里详细找到这些信息: Java SE 9:不可变列表的工厂方法 。

That’s it all about “Java SE 9: Factory Methods for Immutable Map and Map.Entry” concept. We will discuss some more Java SE 9 New Features in my coming posts.

关于“ Java SE 9:不可变地图和Map.Entry的工厂方法”的概念就这么简单。 我们将在以后的文章中讨论更多Java SE 9新功能。

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

如果您喜欢我的帖子或有任何问题/建议/类型错误,请给我评论。

Thank you for reading my tutorials.

感谢您阅读我的教程。

Happy Java SE 9 Learning!

Java SE 9学习愉快!

翻译自: https://www.journaldev.com/13057/javase9-factory-methods-immutable-map

Java SE 9:不可变Map和Map.Entry的工厂方法相关推荐

  1. Java啤酒生产系统描述_Java描述设计模式(03):工厂方法模式

    一.工厂方法模式 1.生活场景 系统常见的数据导出功能:数据导出PDF.WORD等常见格式. 2.工厂方法模式 是类的创建模式,又叫做虚拟构造子(Virtual Constructor)模式或者多态性 ...

  2. java headless_在Java SE上使用Headless模式的超级指南

    这篇文章介绍怎样在标准Java(Java SE,也称作J2SE)平台上用Headless模式. Headless模式是在缺少显示屏.键盘或者鼠标时的系统配置.听起来不可思议,但事实上你可以在这中模式下 ...

  3. java 方法 示例_Java 9示例–收集的工厂方法–创建不可修改的列表,集合和映射...

    java 方法 示例 大家好,这是我在该博客上发表的有关Java 9功能的第一篇文章,今天您将了解我最喜欢的功能"收集的工厂方法" ,它是JEP 269的一部分.JEP代表JDK增 ...

  4. java 静态缓存示例_Java 9 JShell示例:集合静态工厂方法

    java 静态缓存示例 这篇文章继续从My My Java 9 Features博客文章中探索Java9功能. 在这里,我们在List,Set和Map接口中试验Java9 Collections静态工 ...

  5. Java 9示例–收集的工厂方法–创建不可修改的列表,集合和映射

    大家好,这是我在该博客上发表的有关Java 9功能的第一篇文章,今天您将了解我最喜欢的功能"收集的工厂方法" ,它是JEP 269的一部分.JEP代表JDK增强建议. 如果您曾经在 ...

  6. Java 9 JShell示例:集合静态工厂方法

    这篇文章继续了My My Java 9 Features博客文章中对Java9功能的探索. 在这里,我们在List,Set和Map接口中试验Java9 Collections静态工厂方法. 集合静态工 ...

  7. java 静态工厂方法代替构造器的好处

    Java 的静态工厂方法 序:什么是静态工厂方法 Effective Java 2.1 静态工厂方法与构造器不同的第一优势在于,它们有名字 2.2 第二个优势,不用每次被调用时都创建新对象 2.3 第 ...

  8. Java 的静态工厂方法

    序:什么是静态工厂方法 Effective Java 2.1 静态工厂方法与构造器不同的第一优势在于,它们有名字 2.2 第二个优势,不用每次被调用时都创建新对象 2.3 第三个优势,可以返回原返回类 ...

  9. Java SE基础知识详解第[12]期—集合(Set、Collections、Map、集合嵌套)

    写在前面: 每一个不曾起舞的日子,都是对生命的辜负. 希望看到这里的每一个人都能努力学习,不负韶华,成就更好的自己. 以下仅是个人学习过程中的一些想法与感悟,Java知识博大精深,作为初学者,个人能力 ...

最新文章

  1. edgesForExtendedLayout
  2. burpsuite https 社区版_微软推出Visual Studio 2019 RC版 正式版预计四月到来
  3. 安装TokuDB引擎
  4. CORS——跨域请求那些事儿
  5. python获取字典长度_Python基础-字典
  6. MySQL的多表查询(笛卡尔积原理)
  7. PYTHON2.day06
  8. 科学和工程中的信号处理
  9. 40题计算机程序设计基础(C语言)编程习题
  10. Java输出各种乘法口诀表
  11. 帮助台与服务台有何区别,你知道吗?
  12. 计算机二级考试场次是随机的,计算机二级考试知多少
  13. Python cv2读取/存储图片中含中文路径失败的解决方法
  14. 75-CentOS-Docker构建nginx镜像
  15. 怎么挖掘长尾关键词 SEO关键词挖掘方法教程
  16. word段落居中的快捷键_Word段落设置中的小技巧和快捷键推荐
  17. 详细理解 https 单向认证和双向认证原理
  18. python中的scipy基础知识_python中SciPy是什么?
  19. 夏普液晶电视红灯闪,不开机解决方法(最后一般都会故障进厂维修)
  20. YOLOv2---优图代码+实现细节

热门文章

  1. 通用的linux下安装配置svn独立服务
  2. Linux 文件内容替换命令
  3. Java实践(五)——类的声明与引用
  4. flex怎么设置调用的外部浏览器
  5. [转载] python 字符串(string)
  6. [转载] 用python统计中文字符数_使用Python统计字符串中各种字符的个数
  7. [转载] 比较器(Comparable和Comparator)、自然排序、定制排序
  8. python里我最容易搞不清楚问题之一的encode和decode
  9. 为 Joomla 而生的 Kunena 论坛安装手册
  10. DotNetBar 中Ribbon汉化