question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Please document (lack of) equality guarantees for ImmutableMap.values()

See original GitHub issue

ImmutableMap already documents that values() iterates the values in the order that entries were added to the map. However, the return type is ImmutableCollection, which does not have any particular equality guarantees per se (it inherits the non-guarantees from Collection).

I would have assumed that for any two ImmutableMap instances that contained equal values added in the same order, values() would return collections that would compare equal. However, this does not appear to be guaranteed (and appears to definitely be incorrect for some implementations, like ImmutableMapValues, which have no equals() method).

Assuming no guarantees are intended, would it be reasonable to add a note to values() pointing out that — unlike entrySet() and keySet() — there is no guarantee that the collection returned from values() has a sensible equality implementation (and perhaps pointing at values().asList() as a (usually cheap) way to get a collection that does).

(I guess this applies to the values() methods in ImmutableMultimap, ImmutableSortedMap, and ImmutableTable too?)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Meijuhcommented, Jan 13, 2020

I would like to note the importance of solving this issue. The following simple code already fails, without clear documentation why:

final ArrayListMultimap<String, String> alm1 = ArrayListMultimap.create();
final ArrayListMultimap<String, String> alm2 = ArrayListMultimap.create();
assert alm1.values().equals(alm2.values());
0reactions
malcolmrcommented, Aug 30, 2018

Good point about ErrorProne (this would be https://errorprone.info/bugpattern/UndefinedEquals, I assume?).

Worth noting that that check presently won’t trigger for ImmutableCollection, since it requires the receiver type to be exactly Collection (or Iterable, etc). I guess we could add it easily enough, though?

And yes, the “mostly this will work except where it doesn’t” aspect was the part that was troubling me here.

Would it be worth adding @Deprecated ImmutableCollection.equals() with a pointer to asList()? (Or is that only warranted for methods like ImmutableList.add() that are always an error?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

ImmutableMap (Guava: Google Core Libraries for Java 23.0 API)
Returns an immutable collection of the values in this map, in the same order that they appear in entrySet . Methods inherited from...
Read more >
Java features should be preferred to Guava
When migrating an application to Java 8 or even when starting a new one, it's recommended to prefer Java 8 APIs over Guava...
Read more >
fast_immutable_collections | Dart Package - Pub.dev
Global and local configurations that alter how your immutable collections behave with respect to equality, sorting, caching, and flushing. Optional deep ...
Read more >
Google-provided Dataflow streaming templates
Google provides a set of open-source Dataflow templates. These Dataflow templates can help you solve large data tasks, including data import, data export, ......
Read more >
Dataset (Spark 3.3.1 JavaDoc)
as[Person] // Scala Dataset<Person> people = spark.read().parquet("...").as(Encoders.bean(Person.class)); // Java. Datasets can also be created through ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found