4 ms·
And java doesn't have them either. You need to use an external library, likely the google containers library.
by user5994461 9y ago
And java doesn't have them either. You need to use an external library, likely the google containers library.
- Sharlin 9y agoJava does have unmodifiable wrappers but they implement the same basic mutable Map/List/etc. interfaces, just throwing at runtime if mutating methods are called. Not exactly optimal but better than nothing.
- fauigerzigerk 9y agoBetter than nothing but worse than the C++ solution of returning a const reference which can be checked at compile time. Both are just immutable views on top of mutable containers. But in both C++ and Java it is trivial to create a truly immutable container of T. Not so in Go.
- kuschku 9y agoBut you can build and use generic immutable collections.
- humanrebar 9y agoThis is true. My beef with Java collections is the type-tree-gymnastics library authors have to do to get the interfaces exposed properly. Generally people end up with a family of ImmutableContainers and a family of Containers that parallel each other. The mutable Containers usually inherit from the immutable versions. This is possible, but it's an unfortunate form of code duplication.