手机网站标准字体大小,网上做网站网站代理,网站标题被别人改了 应该怎么办,中国建筑集团有限公司官网测评网址java 静态缓存示例这篇文章继续从My My Java 9 Features博客文章中探索Java9功能。 在这里#xff0c;我们在List#xff0c;Set和Map接口中试验Java9 Collections静态工厂方法。 集合静态工厂方法 Java9使用其新的静态工厂方法使创建不可变列表变得更加容易 有12种Set.of和… java 静态缓存示例 这篇文章继续从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.htmljava 静态缓存示例