博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java SE 9:不可变Map和Map.Entry的工厂方法
阅读量:2534 次
发布时间:2019-05-11

本文共 10396 字,大约阅读时间需要 34 分钟。

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

  • Introduction

    介绍
  • Java SE 8: Immutable Empty Map

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

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

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

    Java SE 9:具有Map.of()的不可变非空映射
  • Java SE 9: Immutable Map.ofEntries() Utility

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

    Java 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新功能的几篇文章。 在阅读这篇文章之前,请阅读它们。 这是我前两个帖子的延续:

  • .

  • .

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
emptyMap = new HashMap<>(); Map
immutableEmptyMap = Collections.unmodifiableMap(emptyMap);

Test this code with Java SE 9 REPL:

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

jshell> Map
emptyMap = new HashMap<>()emptyMap ==> {}jshell> Map
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 
Map
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
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
nonemptyMap = new HashMap<>(); nonemptyMap.put(1,"one") nonemptyMap.put(2,"two") nonemptyMap.put(3,"three") Map
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
nonemptyMap = new HashMap<>()nonemptyMap ==> {}jshell> nonemptyMap.put(1,"one")$2 ==> nulljshell> nonemptyMap.put(2,"two")$3 ==> nulljshell> nonemptyMap.put(3,"three")$4 ==> nullonemptyMap)
immutableNonEmptyMap = Collections.unmodifiableMap(nimmutableNonEmptyMap ==> {1=one, 2=two, 3=three}jshell> nonemptyMapnonemptyMap ==> {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 
Map
of(K k1, V v1) static
Map
of(K k1, V v1, K k2, V v2) static
Map
of(K k1, V v1, K k2, V v2, K k3, V v3) static
Map
of(K k1, V v1, K k2, V v2, K k3, V v3) static
Map
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) static
Map
of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) static
Map
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
Map
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
Map
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
Map
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
Map
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 
Map
of(K k1, V v1)

Non-Empty Map Example:-

非空地图示例:-

jshell> Map
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 
Map
ofEntries(Map.Entry
... 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
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.entryjshell> Map
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 
Map.Entry
entry(K k, V v)

Map.entry() method Example:-

Map.entry()方法示例:-

jshell> Map.Entry
immutableMapEntry1 = Map.entry(1,"one")immutableMapEntry1 ==> 1=onejshell> Map.Entry
immutableMapEntry2 = Map.entry(2,"two")immutableMapEntry2 ==> 2=twojshell> Map.Entry
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
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: .

注意:-

不可变映射的特征类似于不可变列表。 您可以在这里详细找到这些信息: 。

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学习愉快!

翻译自:

转载地址:http://qqmzd.baihongyu.com/

你可能感兴趣的文章