#equals

List.remove() oddities

From reddit’s r/java, a user observed that List.remove(Object) removes the first object whose equals() method returns true. Thus, if two objects have object equality with each other, but you wish to remove the second, you’re going to have to find it and return it with the indexed version of remove(int) instead of the remove(Object) call.yawkat observed that you can use Collection.removeIf() (which List inherits) and get...

1 minute(s) read

The JavaChannel Podcast, Episode XVII (17)

The XVIIth podcast is live! … one wonders why the Roman Empire didn’t last much longer, doesn’t one. One also wonders how long one can refer to oneself in the whatever-person this is. (It’s… second-person? No, folks, it’s “third person.” English grammer fumbling and lesson over.) In this podcast, we chase a few things: the discussion topic centers back around on Python vs. Java, and why one might encounter one...

3 minute(s) read

Guava's Equivalence: strategy for equals()/hashCode()

Introduction Given a class C, if you want to implement ordering, you have two choices: Have your class implement Comparable<c> (or Comparable<x>, where X is a superclass of C). Define a Comparator<c> to use with this class (or a Comparator<x>, where X is a superclass of C). In fact, many classes in the JDK will have you supply a Comparator if your class...

4 minute(s) read