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

java java se

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

  • Introduction

    介绍
  • Java SE 8: Immutable Empty Set

    Java SE 8:不可变的空集
  • Java SE 9: Immutable Empty Set

    Java SE 9:不可变的空集
  • Java SE 8: Immutable Non-Empty Set

    Java SE 8:不可变的非空集
  • Java SE 9: Immutable Non-Empty Set

    Java SE 9:不可变的非空集
  • Java SE 9: Immutable Non-Empty Set With Var-Args

    Java SE 9:具有Var-Args的不可变非空集

介绍 (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 sixth 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 post: Java SE 9: Factory Methods for Immutable List.

我已经发表了有关Java SE 9新功能的几篇文章。 在阅读这篇文章之前,请阅读它们。 这是我上一篇文章的延续: Java SE 9:不可变列表的工厂方法

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

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

Java SE 8:不可变的空集 (Java SE 8: Immutable Empty Set)

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

在Java SE 8和更早版本中,如果我们要创建一个空的Immutable或Unmodifiable Set,则应使用Collections类实用程序方法: unmodifiableSet ,如下所示:

Empty Set Example:-

空集示例:-

Set<String> emptySet = new HashSet<>();
 Set<String> immutableSet = Collections.unmodifiableSet(emptySet);

Here we can observe that it is very tedious and verbose process. Let us see the same thing in Java SE 9 now.

在这里,我们可以看到这是一个非常繁琐而冗长的过程。 现在让我们在Java SE 9中看到同样的事情。

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版本都是相同的。

Java SE 9:不可变的空集 (Java SE 9: Immutable Empty Set)

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

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

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

如果通过Java SE 9 Set API,我们可以在Set接口中找到以下方法签名。
空集API实用程序

static <E> Set<E> of()

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

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

Empty Set Example:-

空集示例:-

Set<String> immutableSet = Set.of();

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

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

Java SE 8:不可变的非空集 (Java SE 8: Immutable Non-Empty Set)

In this section, we will see how to create Immutable Non-Empty Set in Java SE 8 and earlier Versions. We use same unmodifiableSet method from Collections class to create an Immutable Non-Empty Set as shown below:

在本节中,我们将看到如何在Java SE 8和更早版本中创建不可变的非空集。 我们使用Collections类中相同的unmodifiableable方法创建一个不可变的非空集,如下所示:

Non-Empty Set Example:-

非空集示例:-

Set<String> nonemptySet = new HashSet<>();
 nonemptySet.add("one");
 nonemptySet.add("two");
 nonemptySet.add("three");
 Set<String> immutableSet = Collections.unmodifiableSet(nonemptySet);

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版本都是相同的。

Here we can observe that it is very tedious and verbose process. Let us see the same thing in Java SE 9 now.

在这里,我们可以看到这是一个非常繁琐而冗长的过程。 现在让我们在Java SE 9中看到同样的事情。

Java SE 9:不可变的非空集 (Java SE 9: Immutable Non-Empty Set)

In this section, we will see how to create Immutable Non-Empty Set in Java SE 9 Version. If we go through the Java SE 9 Set Interface API, we will see a set of overloaded methods as shown below:

在本节中,我们将看到如何在Java SE 9版本中创建不可变的非空集。 如果我们通过Java SE 9 Set Interface API,我们将看到一组重载方法,如下所示:

Non-Empty Set API Utility

非空集API实用程序

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

Non-Empty Set Example:-

非空集示例:-

Set<String> immutableSet = Set.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> Set<String> immutableSet = Set.of("one","two","three")
immutableSet ==> [one, two, three]

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

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

Java SE 9:具有Var-Args的不可变非空集 (Java SE 9: Immutable Non-Empty Set With Var-Args)

Oracle Corp has introduced one more Set.of() method with Var-args syntax. The following is a Var-Args method (Variable Number of arguments method) used to create an Non-empty Immutable Set in Java SE 9:

Oracle Corp引入了另一种具有Var-args语法的Set.of()方法。 以下是用于在Java SE 9中创建非空不可变集的Var-Args方法(可变参数数目方法):

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

It is used to create an Immutable Set with array of elements as shown below:

它用于创建具有元素数组的不可变集,如下所示:

Non-Empty Set Example:-

非空集示例:-

String[] nameArr =  { "one", "two", "three"};
 Set<String[]> set= Set.<String[]>of(nameArr);

Test this with Java SE 9 REPL:

使用Java SE 9 REPL对此进行测试:

jshell> String[] nameArr =  { "one", "two", "three"}
nameArr ==> String[3] { "one", "two", "three" }

jshell> Set<String[]> set = Set.<String[]>of(nameArr);
set ==> [[Ljava.lang.String;@56ef9176]

jshell> set
set ==> [[Ljava.lang.String;@56ef9176]

Here we have created an Immutable Set with Array of String Elements.

在这里,我们创建了带有字符串元素数组的不可变集。

NOTE:-
Characteristics of a Immutable Set 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 Set” 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/12984/javase9-factories-for-immutable-set

java java se

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值