Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List.of(...) or Collections.unmodifiableList()

If you have a List<String> strings instance, would you keep writing:

Collections.unmodifiableList(strings)

or switch to:

List.of(strings.toArray(new String[strings.size()]))

What's the initial impact in performance (memory- and runtime-wise) of instantion? Is there a runtime benefit in the List.of variant?

like image 960
Sormuras Avatar asked Sep 08 '16 08:09

Sormuras


People also ask

What is collections unmodifiableList?

The unmodifiableList() method of Java Collections class is used to get an unmodifiable view of the specified list. If any attempt occurs to modify the returned list whether direct or via its iterator, results in an UnsupportedOperationException.

What is the use of Unmodifiable list in Java?

The unmodifiableList() method of java. util. Collections class is used to return an unmodifiable view of the specified list. This method allows modules to provide users with “read-only” access to internal lists.

How do you make an ArrayList Unmodifiable in Java?

The unmodifiable view of the specified ArrayList can be obtained by using the method java. util. Collections. unmodifiableList().

How do I change Unmodifiable list?

The solution to this problem is quite simple and is highlighted in the following code. final List<String> modifiable = new ArrayList<>(); modifiable. add("Java"); modifiable. add("is"); // Here we are creating a new array list final List<String> unmodifiable = Collections.

What is the use of unmodifiablelist?

It is the list for which an unmodifiable view is to be returned. The unmodifiableList () method returns an unmodifiable view of the specified list. System.out.print ("Unmodifiable List after adding element to the list:");

How to get an unmodifiable view of the specified list?

The unmodifiableList () method of Java Collections class is used to get an unmodifiable view of the specified list. If any attempt occurs to modify the returned list whether direct or via its iterator, results in an UnsupportedOperationException.

What is the return type of unmodifiable list?

Collections unmodifiableList () method returns an unmodifiable “read-only” view of the specified list. Any attempt to modify the returned list directly or via its iterator will result in an UnsupportedOperationException. However, any changes made to the original list will be reflected in the unmodifiable list.

How to get the unmodifiable copy of the source list?

If you need unmodifiable and immutable list or in other words unmodifiable copy of the source list without any dependency to other libraries try this: Collections.unmodifiableList(Collections.list(Collections.enumeration(sourceList)))


2 Answers

This is not really a good comparison because these approaches do different things:

  • Collections::unmodifiable... creates an unmodifiable view. It is not immutable because it changes if you're changing the original, backing collection (list in your example).
  • ...::of on the other hand, creates an immutable copy. Changing the original list will not affect it.

From a performance view it is obvious that creation of the unmodifiable wrapper is cheaper because it only creates one instance with a single field. The new factory methods will create at least one object, maybe backed by an array (if you have three or more elements), that it needs to copy into.

Access could be faster on the new immutable collections but that would have to be benchmarked.

But correctness trumps performance. What do you need? If you need an immutable copy, use the new methods (or Guava's Immutable..., which I would prefer). If you need something immutable, use unmodifiable... and throw away the original (and make sure it stays like that). If you need a view that your caller can not edit, use unmodifiable....

like image 169
Nicolai Parlog Avatar answered Oct 04 '22 18:10

Nicolai Parlog


According to JEP 269 (Convenience Factory Methods for Collections):

Goals

Provide static factory methods on the collection interfaces that will create compact, unmodifiable collection instances. The API is deliberately kept minimal.

Non-Goals

  • It is not a goal to provide a fully-general "collection builder" facility that, for example, lets the user control the collection implementation or various characteristics such as mutability, expected size, loading factor, concurrency level, and so forth.

  • It is not a goal to support high-performance, scalable collections with arbitrary numbers of elements. The focus is on small collections.

  • It is not a goal to provide unmodifiable collection types. That is, this proposal does not expose the characteristic of unmodifiability in the type system, even though the proposed implementations are actually unmodifiable.

  • It is not a goal to provide "immutable persistent" or "functional" collections.

like image 27
Andrew Tobilko Avatar answered Oct 04 '22 18:10

Andrew Tobilko