Showcases the behavior of various List
, Set
and Map
implementations and their
differences.
Some highlights are given in this readme. Refer to the unit tests for all the details.
Consider the following blocks:
List<Integer> list1 = Stream.of(1, 2, 3)
.collect(Collectors.toList()); // Since Java 8
List<Integer> list2 = Stream.of(1, 2, 3)
.collect(Collectors.toUnmodifiableList()); // Since Java 10
List<Integer> list3 = Stream.of(1, 2, 3)
.toList(); // Since Java 16
Is there any difference?
Collectors.toCollection(ArrayList::new)
if you need to make more changes to a List created by a Stream.list2.contains(null)
, an
exception will be thrown by the list.list3.contains(null)
is fine.Set<Integer> origin = new LinkedHashSet<>(Arrays.asList(1, 2, 3));
Set<Integer> set1 = Collections.unmodifiableSet(origin);
Set<Integer> set2 = Set.copyOf(origin);
Set<Integer> set3 = ImmutableSet.copyOf(origin);
origin
is changed afterwards, set2
and set3
will not change. Collections#unmodifiableSet
produces a set that delegates to the origin; set1
cannot be modified directly but any changes to origin
are
still reflected. It is unmodifiable but not immutable.set2.contains(null)
).