java java se_Java SE 9:不可变列表的工厂方法

java java se

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

  • Introduction

    介绍
  • Java SE 8: Empty Immutable List

    Java SE 8:空的不可变列表
  • Java SE 9: Empty Immutable List

    Java SE 9:空的不可变列表
  • Java SE 8: Non-Empty Immutable List

    Java SE 8:非空不可变列表
  • Java SE 9: Non-Empty Immutable List

    Java SE 9:非空不可变列表
  • Characteristics of Immutable List

    不可变表的特征

介绍 (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 forth 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 posts, please read them below :

我已经发表了有关Java SE 9新功能的几篇文章。 在阅读这篇文章之前,请先阅读以下内容:

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

在本文中,我们将讨论一些Java SE 9新功能: “不可变列表的工厂方法” ,其中包含一些简单而合适的示例。

Java SE 8:空的不可变列表 (Java SE 8: Empty Immutable List)

In Java SE 8 and earlier Versions, if we want to create an empty Immutable or Unmodifiable List, we should use Collections class utility method: unmodifiableList as shown below:

在Java SE 8和更早版本中,如果我们要创建一个空的不可变或不可修改列表,则应使用Collections类实用程序方法: unmodifiableList ,如下所示:

Example:-

例:-

List<String> emptyList = new ArrayList<>();
 List<String> immutableList = Collections.unmodifiableList(emptyList);

NOTE:- Diamond Operator does NOT work in Java SE 6 and earlier versions. Rest of the code is same for all Java versions.

注意: -Diamond Operator在Java SE 6和早期版本中不起作用。 其余代码对于所有Java版本都是相同的。

Test the above code in Java SE 9 REPL:

在Java SE 9 REPL中测试以上代码:

Here we can observe that to create an empty Immutable List, we need to do lot of stuff as shown above. It is very tedious and verbose thing right. Let us see the same thing in Java SE 9 now.

在这里,我们可以看到要创建一个空的不可变列表,我们需要做很多事情,如上所示。 这是非常繁琐和冗长的事情。 现在让我们在Java SE 9中看到同样的事情。

NOTE:- For simplicity and quick testing purpose, I’m using Java SE 9 REPL (JShell). That’s why we need an REPL for each language to test new feature quickly without doing much work.

注意:-为了简化和快速测试,我使用Java SE 9 REPL(JShell)。 这就是为什么我们需要每种语言都使用REPL来快速测试新功能而无需做很多工作的原因。

Java SE 9:空的不可变列表 (Java SE 9: Empty Immutable List)

To overcome those shortcomings, Java SE 9 has introduced a couple of useful methods in List interface so that we do not need to use all these tedious steps to create Immutable Empty List.

为了克服这些缺点,Java SE 9在List接口中引入了两个有用的方法,因此我们不需要使用所有这些繁琐的步骤来创建不可变的空列表。

If we go through the Java SE 9 List API, we can find the below method signature in List interface.

如果通过Java SE 9 List API,我们可以在List接口中找到以下方法签名。

static <E> List<E> of()

It is used to create an empty Immutable List (a List with zero elements).

它用于创建一个空的不可变列表(具有零元素的列表)。

Example:-

例:-

List<String> immutableList = List.of();

Test the above code in Java SE 9 REPL:

在Java SE 9 REPL中测试以上代码:

Here we can observe that it is very easy to create an empty Immutable List in Java SE 9.

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

Java SE 8:非空不可变列表 (Java SE 8: Non-Empty Immutable List)

Now we will see how to create Non-Empty Immutable List in Java SE 8 and earlier versions.

现在,我们将看到如何在Java SE 8和更早版本中创建非空不可变列表。

Example:-

例:-

List<String> list = new ArrayList<>();
 list.add("one");
 list.add("two");
 list.add("three");
 List<String> immutableList = Collections.unmodifiableList(list);

Here also we can observe that to create a non-empty Immutable List, 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:非空不可变列表 (Java SE 9: Non-Empty Immutable List)

To overcome those shortcomings, Java SE 9 has introduced the following useful overloaded methods.

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

These useful methods are used to create a new Immutable List with one element to 10 elements:

这些有用的方法用于创建一个新的不可变列表,其中包含一个元素到10个元素:

static <E> List<E> of(E e1)
static <E> List<E> of(E e1,E e2)	
static <E> List<E> of(E e1,E e2,E e3)
static <E> List<E> of(E e1,E e2,E e3,E e4)
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5)	
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6)	
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7)	
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8)	
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9)	
static <E> List<E> of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9,E e10)

The following is a Var-Args method (Variable Number of arguments method):

以下是Var-Args方法(可变参数数目方法):

static <E> List<E> of(E... elements)

Example:-

例:-

List<String> immutableList = List.of("one","two","three");

It is very simple and no-verbosity in this code right. Very nice stuff!

此代码非常简单,没有任何歧义。 好东西!

Test the above code in Java SE 9 REPL:

在Java SE 9 REPL中测试以上代码:

jshell> List<String> immutableList = List.of("one","two","three")
immutableList ==> [one, two, three]

不可变表的特征 (Characteristics of Immutable List)

As we know, Immutable Lists are not modifiable (unmodifiable collections). In this section, we will discuss the Characteristics of Immutable List(They are same in all Java versions.):

众所周知,不可变列表是不可修改的(不可修改的集合)。 在本节中,我们将讨论不可变列表的特征(在所有Java版本中都是相同的):

  • They are Immutable.

    它们是不可变的。
  • We cannot add, modify and delete their elements.

    我们无法添加,修改和删除其元素。
  • If we try to perform Add/Delete/Update operations on them, we will get UnsupportedOperationException as shown below:

    如果尝试对它们执行添加/删除/更新操作,则将得到UnsupportedOperationException,如下所示:
jshell> immutableList.add("four")
|  java.lang.UnsupportedOperationException thrown: 
|        at ImmutableCollections.uoe (ImmutableCollections.java:68)
|        at ImmutableCollections$AbstractImmutableList.add (ImmutableCollections.java:74)
|        at (#2:1)
  • They don’t allow null elements.

    他们不允许使用null元素。
  • If we try to create them with null elements, we will get NullPointerException as shown below:

    如果尝试使用null元素创建它们,则将获得NullPointerException,如下所示:
  • jshell> List>String> immutableList = List.of("one","two","three", null)
    |  java.lang.NullPointerException thrown: 
    |        at Objects.requireNonNull (Objects.java:221)
    |        at ImmutableCollections$ListN. (ImmutableCollections.java:179)
    |        at List.of (List.java:859)
    |        at (#4:1)
  • If we try add null elements, we will get UnsupportedOperationException as shown below:

    如果尝试添加null元素,则会得到UnsupportedOperationException,如下所示:
  • jshell> immutableList.add(null)
    |  java.lang.UnsupportedOperationException thrown: 
    |        at ImmutableCollections.uoe (ImmutableCollections.java:68)
    |        at ImmutableCollections$AbstractImmutableList.add (ImmutableCollections.java:74)
    |        at (#3:1)
  • They are serializable if all elements are serializable.

    如果所有元素都是可序列化的,则它们是可序列化的。
  • That’s it all about “Java SE 9: Factory Methods for Immutable List” concept. We will discuss some more Java SE 9 New Features in my coming posts.

    这就是“ Java SE 9:不可变列表的工厂方法”概念的全部内容。 我们将在以后的文章中讨论更多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/12942/javase9-factory-methods-immutable-list

    java java se

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值