Why might changing a field from int to Integer cause objects to disappear from Sets?

267
December 14, 2017, at 09:21 AM

There's a presentation on AutoValue (a terse way to define immutable objects in Java, with sensible defaults for equals, hashCode, etc.): https://docs.google.com/presentation/d/14u_h-lMn7f1rXE1nDiLX0azS3IkgjGl5uxp5jGJ75RE/edit#slide=id.g2a5e9c4a8_047

In the beginning, they go over some alternatives to AutoValue. Obviously, one alternative is a POJO coded by hand. They warn about this POJO being unexpectedly hard to change.

I've transcribed the slide here:

Are changes really that risky?

Suppose a required int field becomes optional. You change

int number;

to

Integer number;

Tests still pass... and everything's fine... until one day, objects put into Sets start mysteriously "disappearing" from the set! Why? (Left as an exercise to the reader.)

I can't figure out this "exercise". What kind of "disappearing"? contains(element) is unexpectedly false? size() decrements without remove()? iterator() doesn't include the element anymore? And why would any of these happen?

Answer 1

AFAIK the main thing that would break is equals, because this.number == obj.number has unexpected results when number is an Integer.

That would affect contains as well as remove (both unexpectedly false), I think.

I'm still a bit green with Java though, so if anyone has other comments or answers, I'd appreciate it. Mostly, I don't think size or iterator would be affected, so presumably the element would still show up in debuggers that use iterator or some fancier way of getting the contents. That doesn't sound much like "disappearing" to me so I'd like to know if I'm missing anything.

Rent Charter Buses Company
READ ALSO
Using Swing Library to Move Buttons

Using Swing Library to Move Buttons

I am trying to create a type of Graphics Editor that allows users to create graphic depictions of American Football playsTo do this, the User should be able to do the following:

281
How to make the run time of a program a constant everytime?

How to make the run time of a program a constant everytime?

I have been finding the size of a stack in java and the run time is different every timeIs there any way the run time of the method be constant? What does run time depend upon? Would it help if the stack had a constant size?

231
Lisp : Don't know how to create ISeq from Java.lang.Integer

Lisp : Don't know how to create ISeq from Java.lang.Integer

I'm trying to open a text file and split it into a list of integer values in ClojureI get this error code every single time, and I've got no idea why

321
Check if String array contains word in another String Array

Check if String array contains word in another String Array

Having trouble figuring out how to check if a word from one String Array is in another String ArrayThis is what I have so far:

222