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

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

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

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
emptySet = new HashSet<>(); Set
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 
Set
of()

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

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

Empty Set Example:-

空集示例:-

Set
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
nonemptySet = new HashSet<>(); nonemptySet.add("one"); nonemptySet.add("two"); nonemptySet.add("three"); Set
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 
Set
of(E e1)static
Set
of(E e1,E e2) static
Set
of(E e1,E e2,E e3)static
Set
of(E e1,E e2,E e3,E e4)static
Set
of(E e1,E e2,E e3,E e4,E e5) static
Set
of(E e1,E e2,E e3,E e4,E e5,E e6) static
Set
of(E e1,E e2,E e3,E e4,E e5,E e6,E e7) static
Set
of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8) static
Set
of(E e1,E e2,E e3,E e4,E e5,E e6,E e7,E e8,E e9) static
Set
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
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
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 
Set
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
set= Set.
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
set = Set.
of(nameArr);set ==> [[Ljava.lang.String;@56ef9176]jshell> setset ==> [[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: .

注意:-

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

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

翻译自:

java java se

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

你可能感兴趣的文章
小D课堂 - 新版本微服务springcloud+Docker教程_3-01 什么是微服务的注册中心
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-03CAP原理、常见面试题
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-05 服务注册和发现Eureka Server搭建实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-06 服务注册和发现之Eureka Client搭建商品服务实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_3-07 Eureka服务注册中心配置控制台问题处理...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-01 常用的服务间调用方式讲解
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-02 微服务调用方式之ribbon实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-03 高级篇幅之Ribbon负载均衡源码分析实战...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-06 Feign核心源码解读和服务调用方式ribbon和Feign选择...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_4-05 微服务调用方式之feign 实战 订单调用商品服务...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-02 Netflix开源组件断路器
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-01分布式核心知识之熔断、降级
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-04 feign结合hystrix断路器开发实战下...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-03 feign结合hystrix断路器开发实战上...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-01 微服务网关介绍和使用场景
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-05熔断降级服务异常报警通知
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_5-08 断路器监控仪表参数
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_6-02 springcloud网关组件zuul
查看>>