- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
根据 doc Collections.unmodifiableList
方法返回指定列表的不可修改 View 。返回的列表真的不可修改吗?不可修改的 View 是什么意思?
根据 doc Collectors.toUnmodifiableList
方法返回一个收集器,该收集器按遇到的顺序将输入元素累积到一个不可修改的列表中。这里返回的列表真的是不可修改的吗?
注意:我所说的可修改是指可以使用set
操作修改 View 。我想了解其中的区别以及它们之间的关系?
最佳答案
Collections.unmodifiableList
方法返回指定列表的不可修改 View 。不可修改的 View 集合是不可修改的集合,也是支持集合的 View 。请注意,对支持集合的更改可能仍然是可能,如果发生,它们通过不可修改的 View 是可见的。
List<String> srcList = Arrays.asList("Apple", "Banana", "Cherry");
var fruits = new ArrayList<>(srcList);
var unmodifiableList = Collections.unmodifiableList(fruits);
fruits.set(0, "Apricot");
var modFruit = unmodifiableList.get(0);
System.out.println(modFruit); // prints Apricot
我们可以在 Java 10 及更高版本中拥有真正的不可变列表。有两种方法可以获取真正不可修改的列表,如下所示:
var unmodifiableList = List.copyOf(srcList);
=> 打印 Applevar unmodifiableList = srcList.stream().collect(Collectors.toUnmodifiableList());
=> 打印 Apple所以 Collectors.toUnmodifiableList
方法返回一个真正的不可修改列表 List.of
在 Java 9 中引入。这个方法返回一个 Collector
其中Collections.unmodifiableList
方法返回一个列表。根据 doc不可修改列表具有以下特点:
- 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.
关于java - Java 10 中的 Collectors.toUnmodifiableList 与 Collections.unmodifiableList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52620446/
以下面的列表为例: List input = List.of("FOO", "FOO", "FOO", "FOO", "FOO", "BAR", "BAR", "BAZ", "BAZ", "BAZ",
我有一个类似于下面的类 MyObject . public class MyObject { private String key; // not unique. multiple objec
我想获取按频率键控的数组值频率图。我能够得到相反的 - 按值键控的 map 。尝试切换参数,但 grouping by 不接受 Collector 作为第一个参数。 另一个问题,如何将 Map 实现更
我是 Java 8 Stream API 的新手,但我想用它来解决以下问题。假设我有一个名为 InputRecord 的 POJO,其中包含 name、fieldA 和 fieldB 属性,这些属性可
基准测试在 intel core i5, Ubuntu 下运行 java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.
我尝试使用流和收集器对值进行分组。我有我必须拆分的字符串列表。 我的数据: List stringList = new ArrayList<>(); stringList.add("Key:1,2,3
我想创建一个 Map来自 List的 Points并在映射中使用相同的 parentId 映射列表中的所有条目,例如 Map> . 我用了Collectors.toMap()但它没有编译: Map>
我已经实现了以下示例: Map> map = events.getItems().stream() .collect(Collectors.groupingBy(Event::getS
这个问题在这里已经有了答案: java 8 Collector is not a functional interface, who can tell why? (2 个回答) Java8: Usin
我想通过 Java 8 Stream 和 Collector 接口(interface)将 Map 转换为 ConcurrentHashMap,然后是我可以使用的两个选项。 第一个: Map mb =
如果我有一个对象列表(~200 个元素),只有几个唯一对象(~20 个元素)。 我只想拥有独特的值(value)。之间list.stream().collect(Collectors.toSet())
对于下面开发的 Java 8 代码,我收到以下错误。在此示例中,尝试将 Dish Name 的所有名称连接到一个变量中。使用下面的代码我得到了这个 "The method collect(Collec
我正在尝试使用rook在kubernetes集群上配置ceph,我已经运行了以下命令: kubectl apply -f common.yaml kubectl apply -f operator.y
这是我的流: Stream> futureStream = IntStream .iterate(1, n -> n n++) .mapToObj(pageNumber -> thi
Collector.of(Supplier 供应商、BiConsumer 累加器、BinaryOperator 组合器、Function 完成器、Characterstics...) Coll
按照常规思维,往一个map里put一个已经存在的key,会把原有的key对应的value值覆盖,然而通过一次线上问题,发现Java8中的Collectors.toMap反其道而行之,它默认给抛异常,
我想通过基于 LocalDateTime 的集合进行分组,但我只想获取小时,而不是分钟、秒... .collect(Collectors.groupingBy(cp -> getUpdateLocal
这个问题在这里已经有了答案: What is the point of "final class" in Java? (24 个答案) 关闭 3 年前。 为什么 Collectors 类在 Java
我有一个College具有嵌套静态类的类 Dept 学院 class College { private String collegeName; private Dept dept; public D
在Java库源代码中,Collectors#toList方法的定义如下: public static Collector> toList() { return new CollectorIm
我是一名优秀的程序员,十分优秀!