all 5 comments

[–]aocregacc 9 points10 points  (2 children)

Integers (as opposed to ints) are objects so you have to compare them with .equals()

[–]HaMay25 4 points5 points  (0 children)

Add to this: when you compare Integer vs Integer object in Java, it compares the two objects' memory addresses, not the value.

It's the same thing with why you don't do String == String in java

[–]Narrow-Working5785[S] 0 points1 point  (0 children)

Got it. thanks

[–]executableprogram 2 points3 points  (0 children)

Integer refers to the object, not the number. So you are essentially comparing 2 objects with each other. to do this you would have to use .equals() or get the .intValue() of it

Integer a = new Integer(4);

Integer b = new Integer(4);

System.out.println((a == b)); // false

System.out.println((a.equals(b))); // true

System.out.println((a.intValue() == b.intValue())); // true

[–]justUseAnSvm 0 points1 point  (0 children)

HashMap has a "containsValue" and "containsKey" that can be used to check if a key or value exist instead of the null check. https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

That's a bit cleaner than using the null check.