Gotchas in Java
Krishna Chaitanya Terala
1 min read
These are some pitfalls that took me some time to unravel.
I used to think
.collect(Collectors.toList())
and.toList()
are same and interchangeable.But first one creates a mutable list and second one creates a immutable list
List<Integer> list = new ArrayList<>(); // Creates a Modifiable List list = list.stream().collect(Collectors.toList()); list.add(1); // This won't throw an error // Creates an Immutable list list = list.stream().toList(); list.add(1); // This throws an error
I will update this blog as soon as I find one.
0
Subscribe to my newsletter
Read articles from Krishna Chaitanya Terala directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by