20个优秀的响应式设计html5网站模板,农村电商平台开发,公司做完网站怎么搜不到,分析seo做的不好的网站这篇文章继续了My My Java 9 Features博客文章中对Java9功能的探索。 在这里#xff0c;我们在List#xff0c;Set和Map接口中试验Java9 Collections静态工厂方法。 集合静态工厂方法 Java9使用其新的静态工厂方法使创建不可变列表更加容易 有12种Set.of和List.of方法– L… 这篇文章继续了My My Java 9 Features博客文章中对Java9功能的探索。 在这里我们在ListSet和Map接口中试验Java9 Collections静态工厂方法。 集合静态工厂方法 Java9使用其新的静态工厂方法使创建不可变列表更加容易 有12种Set.of和List.of方法– List.of或Set.of List.ofE e1或Set.ofE e1至E e10 List.ofE…元素或Set.ofE…元素 例子 jshell Set.of()
$1 []
| created scratch variable $1 : SetObject 注意将推理作为List对象 要静态 清单 E e1E e2E e3的– jshell List.of(one,two,three)
$2 [one, two, three]
| created scratch variable $2 : ListString 将推理记录为列表 宾语 参数的数量一直增加到E e10此时我们可以使用vararg – 静态的 清单 E ...个元素 同样地图定义– staticMapof staticMapofK k1V v1到K k10V v10 staticMapofEntriesMap.Entry …条目–注意使用 地图条目 例子 jshell Map.of()
$12 {}jshell Map.of(key1, value1, key2, value2)
$13 {key1value1, key2value2}
| created scratch variable $13 : MapString,String集合静态工厂方法的特征 这些静态工厂方法列表集合和映射的共同特征是– 从结构上讲是不可变的-尽管元素本身是不可变的但抛出UnsupportedOperationException jshell SetString immutableSet Set.of(one,two,three)
immutableSet [three, two, one]
| created variable immutableSet : SetStringjshell immutableSet.add(four)
| java.lang.UnsupportedOperationException thrown: 没有空-抛出NullPointerException jshell ListObject notNullList List.of(null)
| Warning:
| non-varargs call of varargs method with inexact argument type for last parameter;
| cast to java.lang.Object for a varargs call
| cast to java.lang.Object[] for a non-varargs call and to suppress this warning
| ListObject notNullList List.of(null);
| ^--^
| java.lang.NullPointerException thrown:
| at List.of (List.java:1030)
| at (#10:1) 序列化–如果元素可序列化则序列化 列出特定特征 订单–订单与元素输入保持相同 jshell ListString immutableList List.of(one,two,three)
immutableList [one, two, three]
| created variable immutableList : ListString设置特定特征 拒绝重复–集合还将在创建时使用IllegalArgumentException拒绝重复– jshell Set.of(one,one)
| java.lang.IllegalArgumentException thrown: duplicate element: one地图特定特征 拒绝重复的Keus –映射将拒绝具有IllegalArgumentException的重复键– jshell Map.of(key1, value1, key1, value2)
| java.lang.IllegalArgumentException thrown: duplicate key: key1
| at ImmutableCollections$MapN.init (ImmutableCollections.java:680)
| at Map.of (Map.java:1326)
| at (#15:1) 也不保证迭代 结论 这些是用于创建不可变集合的有用且快速的方法而jshell为了解新方法及其相关特性提供了良好的测试基础。 翻译自: https://www.javacodegeeks.com/2017/10/java-9-jshell-examples-collections-static-factory-methods.html