【双语】JAVA9新特性:不可变集合IMMUTABLE COLLECTIONS

英文原文链接:https://www.javaspecialists.eu/archive/Issue248.html

Exciting news. We now have “immutable collections” in Java 9. Just like Scala (not really).

这真是一个令人兴奋的消息,Java 9 像Scala一样支持了不可变集合.

For example, if we want to create lists, we can do this:

举个例子,如果我们想创建一个不可变list,我们可以像如下这么做:

List list0 = List.of(); // List0
List list1 = List.of(42); // List1
List list2 = List.of(42, 57); // List2
List list3 = List.of(42, 57, 1); // ListN
List list4 = List.of(42, 57, 1, 2); // ListN 

Someone pointed out that the lists returned by Arrays.asList() were also immutable. They are not. Whilst you cannot add and remove elements, you can still set individual elements of the list.

有人说数组用Arrays.asList()转换成list时不也是不可变集合吗?答案是否定的,list依然可以添加和删除元素。

We can create sets in a similar way, like so:

我们可以用类似的方法创建Set集合:

Set set0 = Set.of(); 
Set set1 = Set.of(42);
Set set2 = Set.of(42, 57);
Set set3 = Set.of(42, 57, 1); 
Set set4 = Set.of(42, 57, 1, 2); 

This begs the question: How can we do set operations? Let’s take these sets:

这就引出了一个问题:我们怎么操作这些集合?先让我们创建两个Set不可变集合:

Set one = Set.of(1, 2, 3);
Set two = Set.of(3, 4, 5);

To create a union of these is a bit awkward, since there is no easy way to create an immutable Set from an ordinary Set. We could create a temporary Set and then the union from that, like so:

挺尴尬的我们没有一个简单的方法来实现得到两个不可变集合并集的效果,我们可以创建一个临时集合来达到目的:

Set temp = new HashSet<>(one); temp.addAll(two);
Set union = Set.of(temp.toArray(new Integer[0])); System.out.println("union = " + union); I do not like temporary variables, so we could wrap this in a facade somewhere. To do the intersection is similar:

我并不是很喜欢临时变量,或许我们可以写一个方法来封装这个操作,当然获得交集的方式也类似:

Set temp = new HashSet<>(one); 
temp.retainAll(two); 
Set intersection = Set.of(temp.toArray(new Integer[0])); 
System.out.println("intersection = " + intersection); 

We can also create Maps with Map.of():

我们创建Map不可变集合也是类似的方法:

Map<String, Integer> map0 = Map.of();
Map<String, Integer> map1 = Map.of("one", 1); Map<String, Integer> map2 = Map.of("one", 1, "two", 2); 

Map.of() works with key and value pairs up to 10 entries. Beyond that, we need to pass in a var-args of Entry instances and use Map.ofEntries(Entry…)

Map.of()最多只能放进10个键值对,如果想放入更多的键值对,我们需要用到传入实体参数的方式来创建不可变集合:

Map<String, Integer> mapN = Map.ofEntries(Map.entry("one", 1),
Map.entry("two", 2), Map.entry("three", 3),
Map.entry("four", 4),
Map.entry("five", 5)); 

It is, of course, not nearly as convenient as Kotlin/Swift/Scala/Groovy/etc. It’s a very small addition to Java which you get when you go over to Java 9.

当然了,java的不可变集合使用上还是不如Kotlin/Switf/Scala/Groovy/等等那么简单,他只是一个Java9所有新功能中的一小部分。

Kind regards from Crete

Heinz

英文原文链接:https://www.javaspecialists.eu/archive/Issue248.html

转载于:https://my.oschina.net/adminLogin/blog/3053593

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值