1

I'm searching for a java library for collections with no methods that allow for mutations. effectively immutable read only collections.

By that I mean, NO METHODS. Not like the usual Java immutable collections that have methods like add or remove that throw an exception when called. No... I want the compiler to let me know I'm trying to do something not allowed, instead of some error at runtime.

I know it exists because I've used it, but I cannot remember the name of such library.

caeus
  • 3,084
  • 1
  • 22
  • 36
  • Write a wrapper class! But whatever you use will not be a `Collection` since *all* `Collection`s shall declare those methods because it's part of the interface of a `Collection` – xtratic Feb 04 '21 at 18:38
  • @xtratic I know such library exists. Also, my company won't allow me the space to implement said wrappers. – caeus Feb 04 '21 at 18:40
  • Wrappers will not help as it will not give compile time error. – Thiyanesh Feb 04 '21 at 18:43
  • @Horse if you make a wrapper that only exposes "read" methods.. then it certainly will give compile errors when you try to use "write" methods which aren't exposed/defined. – xtratic Feb 04 '21 at 18:44
  • 1
    @AlejandroNavas Please use extra caution in choosing a collection framework as it will have very big impact if your interfaces/modules are going to be used company. Further with certain incompatible library versions over years, there are projects that have resorted to shading specific versions. Again not discourage, but please use extra caution while choosing a collection framework with short focus. – Thiyanesh Feb 04 '21 at 18:58
  • @xtratic, Agreed. If the word collection is used in abstract sense, with a different class hierarchy it can achieved. My initial response was from a code perspective. Thank you. – Thiyanesh Feb 04 '21 at 18:59

2 Answers2

2

Eclipse Collections: https://www.eclipse.org/collections/

Their own guide on immutable collections says:

All of the basic containers in Eclipse Collections have interfaces for both mutable and immutable (unchangeable) forms. This departs somewhat from the JCF model, in which most containers are mutable.

An immutable collection is just that - once created, it can never be modified, retaining the same internal references and data throughout its lifespan. An immutable collection is equal to a corresponding mutable collection with the same contents; a MutableList and an ImmutableList can be equal.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • 1
    THIS ONES! thanks a lot! This is what I was searching for. I don't know why I forgot the name – caeus Feb 04 '21 at 18:47
  • 1
    As you well found, Guava `ImmutableCollection` has mutation methods which throw. I also looked at Trove, hppc, fastutil, koloboke, and Javolution, and none of those offers what you want. – Petr Janeček Feb 04 '21 at 18:50
  • Thanks @Petr! Still gonna check the other libraries – caeus Feb 04 '21 at 18:51
  • 1
    All that said, I myself in my code only use Guava's ImmutableCollections as they strike a good balance and are interoperable with all existing code everywhere. Also their mutation methods show warnings upon compilation, which is good enough for us. Eclipse Collections, while very nice, require you to use them and only them wherever possible. – Petr Janeček Feb 04 '21 at 18:53
  • 1
    I'm gonna use Eclipse collections as I'm trying to have my team slowly acquire a functional mindset for programming. Given they have a very imperative mindset, I also don't want some sneaky exceptions thrown at runtime for that. Fortunately mutable eclipse collections do implement the Java Collections hierarchy, so I just have to add some .toMutableList() or something like that, to have it interoperate with other libraries – caeus Feb 04 '21 at 18:57
1

Guava's immutable collections declare mutable methods using @Deprecated so their use will emit a compiler warning.

This is probably the best of both worlds, since it allows passing an ImmutableCollection wherever a Collection is expected.

meriton
  • 68,356
  • 14
  • 108
  • 175
  • I honestly prefer to create a usual java list out of an immutable Eclipse collection – caeus Feb 04 '21 at 18:50
  • @meriton, `ImmutableCollection` will not guarantee the compile time check as expected by OP(even advanced code scanner can not carry forward deeply nested implementations abstracting the actual instantiated object). This clearly requires a tradeoff between interface compatibility with java `Collection` or the `compile time check`. – Thiyanesh Feb 04 '21 at 19:03