- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
Java 10 的发布带来了新的静态工厂方法,具体来说:
static <E> List<E> copyOf(Collection<? extends E> coll)
static <E> Set<E> copyOf(Collection<? extends E> coll)
static <K,V> Map<K,V> copyOf(Map<? extends K,? extends V> map)
因为这些方法允许我们复制 Collection
s 不同 Collection
实现,它们如何与现有方法进行比较和对比?
最佳答案
正如 List#of
, Set#of
, 和 Map#ofEntries
允许我们在 Java 9 中创建不可修改 实现,copyOf
方法提供了一种从现有 Collection
创建不可修改 实现的便捷方式s 和 Map
s(取决于方法,因为 Map#copyOf
接受 Map
)在 Java 10 中。
这使我们能够轻松地创建一个不可修改 Set<E>
来自List<E>
反之亦然。
不过,这些方法带来了一些注意事项(引用 java.util.List
的 documentation):
- They are unmodifiable. Elements cannot be added, removed, or replaced. Calling any mutator method on the List will always cause UnsupportedOperationException to be thrown. However, if the contained elements are themselves mutable, this may cause the List's contents to appear to change.
- They disallow null elements. Attempts to create them with null elements result in NullPointerException.
- They are serializable if all elements are serializable.
- The order of elements in the list is the same as the order of the provided arguments, or of the elements in the provided array.
- They are value-based. Callers should make no assumptions about the identity of the returned instances. Factories are free to create new instances or reuse existing ones. Therefore, identity-sensitive operations on these instances (reference equality (==), identity hash code, and synchronization) are unreliable and should be avoided.
- They are serialized as specified on the Serialized Form page.
关于 Set#copyOf
的注意事项和 Map#copyOf
,请参阅他们的文档。
关于java - List#copyOf、Set#copyOf 和 Map#copyOf 与传统方法有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49089751/
Java 10 的发布带来了新的静态工厂方法,具体来说: static List copyOf(Collection coll) static Set copyOf(Collection co
这个问题已经有答案了: How to make a deep copy of Java ArrayList [duplicate] (2 个回答) 已关闭 4 年前。 我的问题是我需要复制一个列表,但
我只想将“b”、“d”和“e”从 arr1 复制到 arr2,并在添加时动态增加 arr2 的大小。 我尝试添加一个if条件 if(!arr1[i].equals("a") && !arr1[i].
在看别人的关于copyonwritearraylist 这个类的时候,看到有人提出了关于:数组拷贝的方法arrays.copyof() 的问题,如下: 只是复制了引用地址,数组内的对象还是和旧数组一
我有无数带有自定义键和比较器的 map 。我注意到当我使用类似的代码创建 map 时 var map = TreeMap<>( someCustomComparator ); 然后我使用如下代
假设我有以下 map 定义: Map>> map = Maps.newHashMap(); map.put("a", Arrays.asList(Sets.newHashSet(1, 2, 3), S
我有以下代码: private static final ImmutableMultimap namesToAddress; public static List getAddresses(Strin
我有一个程序,如果整数的行为类似于整数堆栈,则使用数组。但是,对于数组,必须有定义数量的元素。当用户调用诸如 push() 之类的方法并超过初始数组数时,我必须将原始数组中的所有内容分配到更新后的数组
关于 Arrays.copyOf 是否会生成深拷贝或浅拷贝,似乎存在很多困惑和不同的意见([1] 和其他来源)。 此测试表明副本很深: String[] sourceArray = new Strin
我正在为类做作业,这需要我创建一个数组并根据用户的意愿添加到其中。这是我到目前为止所拥有的: public void add(Scanner stdIn) { entries = new St
为什么这个问题不是可能与 How Arrays.asList(int[]) can return List? 重复。这个问题并没有真正回答我的具体情况,因为我试图弄清楚我对 Arrays.copyOf
elementData = Arrays.copyOf(elementData, newCapacity); 给出错误: The method copyOf(Object[], int) is und
Java 并发实践的摘录/片段- @Immutable class OneValueCache{ private final BigInteger lastNumber; privat
关于 Arrays.copyOf 是否会产生深拷贝或浅拷贝似乎存在很多困惑和不同意见([1] 和其他来源)。 这个测试表明副本很深: String[] sourceArray = new String
我正在使用 Guava-05-snapshot 和 Sun 的 JDK 1.6执行此代码段的代码会爆炸: List badpasswords = Lists.newArrayList( Passwor
我想知道这个调用是否是线程安全的。例如,如果我有一组 s: Set s = new HashSet<>(); 还有两个线程A和B,线程A修改集合: for (int i = 0; i newS =
我正在尝试创建一个方法,它几乎可以将任何东西作为参数,并返回一个带有一些分隔符的值的串联字符串表示形式。 public static String getConcatenated(char delim
据我了解,我们可以使用 java.util.Arrays.copyOf复制二维数组,但是将其用于防御性复制是否安全(假设元素是不可变的)?例如 void setValues(Integer[][] a
我有一个关于函数“Arrays.copyOf()”的快速问题。 我有一个 IntArray "rows"和一个 Arraylist "listMoves"。我想创建一个行的副本并将其添加到 listM
函数ArrayList.add()工作得非常快。我检查了源代码,发现实现是Arrays.copyOf() private void grow(int minCapacity) { // ove
我是一名优秀的程序员,十分优秀!