简体   繁体   English

Java Collections.unmodifiableList()时间复杂度

[英]Java Collections.unmodifiableList() time complexity

I am wondering what's the time complexity of Collections.unmodifiableList ? 我想知道Collections.unmodifiableList的时间复杂度是多少?

If I want to make an incoming Collection immutable, what's the cheapest way? 如果我想让传入的Collection不可变,那么最便宜的方法是什么?

I am wondering what's the time complexity of Collections.unmodifiableList ? 我想知道Collections.unmodifiableList的时间复杂度是多少?

Collections.unmodifiableList returns a view of its argument list as opposed to a copy, so it's O(1). Collections.unmodifiableList返回其参数列表的视图而不是副本,因此它是O(1)。 This is typically how you'd create an immutable list, but be sure to not change the original mutable list, since those changes will be reflected in the newly created immutable list. 这通常是您创建不可变列表的方式,但请确保不要更改原始可变列表,因为这些更改将反映在新创建的不可变列表中。 This shouldn't be a problem if you keep the original list private. 如果您将原始列表保密,这应该不是问题。 This is the situation I'm talking about: 这就是我所说的情况:

List<String> modifiableList = new ArrayList<>();
List<String> unmodifiableList = Collections.unmodifiableList(modifiableList);

System.out.println(unmodifiableList);
modifiableList.add("hello");
System.out.println(unmodifiableList);
[]
[hello]

This happens because unmodifiableList is a view (and not a copy) of modifiableList . 发生这种情况是因为unmodifiableListmodifiableList的视图(而不是副本)。

Time complexity for which operations? 操作的时间复杂性?

For all non-modifying operations, it would be in the same O notation as the underlying data structure. 对于所有非修改操作,它将与基础数据结构具有相同的O表示法。

For all modifying operations, it probably would be just a bit faster (early error exiting). 对于所有修改操作,它可能会稍微快一点(早期错误退出)。

Collections.unmodifiableList is good approach to making Collection<> immutable. Collections.unmodifiableList是使Collection<>不可变的好方法。 As well it returns an unmodifiable wrapper; 它还返回一个不可修改的包装器; it does not copy the contents of the input list. 它不会复制输入列表的内容。

So its not slowing down the process and efficient for large object as well. 因此,它不会减慢过程,也不会对大型对象有效。

Collections.unmodifiableList is cheapest way to immutable. Collections.unmodifiableList是不可变的最便宜的方式。

It just return a wrapper list implementation. 它只返回一个包装列表实现。

It wont copy the list. 它不会复制列表。

They override the following method like below. 它们覆盖以下方法,如下所示。

public void add(int index, E element) {
    throw new UnsupportedOperationException();
    }
public E remove(int index) {
    throw new UnsupportedOperationException();
    }

So If any one tries add / delete it will throw exception. 因此,如果任何人尝试添加/删除它将抛出异常。

They override the get method like below 它们覆盖了下面的get方法

public E get(int index) {return list.get(index);}

So it just delegate the actual list to get. 所以它只是委托实际列表来获取。

But It has one drawback. 但它有一个缺点。

List unmodifiable= Collections.unmodifiableList(list);

unmodifiable.remove(0); unmodifiable.remove(0);

it throws exception. 它抛出异常。

If you change the original list It will reflect in unmodifiable also. 如果您更改原始列表它也将以不可修改的形式反映出来。

list.remove(0); list.remove(0);

It will reflect the unmodifiable list also. 它也将反映不可修改的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM