r/learnprogramming • u/tempyreddity • Dec 03 '15
PSA: Don't use the Java standard library
Hey guys,
So I was working through an advent of code challenge, and it looks like apparently there are issues that can arise from the java standard library, in particular hashset. See here: https://www.reddit.com/r/adventofcode/comments/3v8roh/day_3_solutions/cxlfuvp?context=3
I'm a newbie so I thought others would want to know about this before trying to figure out why their programs aren't working. Write your own classes!
13
u/michael0x2a Dec 03 '15 edited Dec 03 '15
Protip: unless you're working with an obscure language or library, you should always assume that any bugs in your code are your fault, not the compiler's or the library.
Java and Java's standard library is used by literally millions, if not billions of programs, and is frequently used in industry by large tech companies (for example, Google uses Java as one of their main languages).
So then, if all these people are using Java successfully + are not running into the same issues you are, the odds are very high that the problem lies with your code, not Java.
(And if you want to argue that Java is at fault, then the burden of proof is on you. You would need to demonstrate or prove that Java is the problem to a high degree of rigor if you want people to take you seriously. For example of what that level of rigor might look like, see this StackOverflow question.)
In this case, the problem is that you've misunderstood how to use HashSet (and possibly how references work in Java). Notice that in your switch-case statement, you're modifying your coord field (which always points at the same Point object), which will end up changing its hash value. Since you're inserting a reference to the exact same Point object into the HashSet with each iteration, translating coord will end up changing the x and y coordinates (and therefore the hash value) of every single item you have inside the set.
This is fairly bad, since your hashset is no longer accurately tracking distinct points, leading to incorrect results.
More broadly, I think the mistake you made is assuming that you're inserting separate and distinct values into the set, or that once you've inserted something that it's "frozen" in some way. This is inaccurate -- your program creates and uses only a single Point object, and it is entirely possible to change something that is inside a hashset.
The main takeaway is that you should be very careful when inserting mutable object into a hashset, or using them as keys in a hashmap. If you perform any operation on that object that causes it to mutate/causes its hash to change, you've suddenly rendered your hashset or hashmap inaccurate.
Once it's in there, leave it alone (or better yet, use immutable types for set elements/map keys to sidestep the problem entirely).
3
u/Vimda Dec 03 '15
The Java Standard Library has been around for quite some time, with many people successfully using it. Do you really think that you've found a bug which no one has noticed? You shouldn't leap to such conclusions before proper investigation.
3
u/nutrecht Dec 03 '15
Haha. Right.
I used that exact same hash set. Guess what: it works fine. Heck; you even got told what you did wrong.
With your attitude you're not going to learn anything.
3
1
u/1100101000 Dec 03 '15
As others have said - your code is wrong. But either way, the correct solution would not be to give up on the standard library, it would be to report the bug so that the developers can fix it.
1
u/zifyoip Dec 03 '15
report the bug so that the developers can fix it
But only after carefully reading and understanding the advice here:
http://www.catb.org/esr/faqs/smart-questions.html#idp42167136
5
u/zifyoip Dec 03 '15
There is nothing wrong with the Java standard library. The error is in your code, not in the standard library. You need to figure out what that error is.
A bad workman blames his tools.