|
Is this some kind of no-google assignment? You should be able to easily search for how to implement hashCode and equals in Java
|
# ¿ Nov 15, 2022 13:07 |
|
|
# ¿ May 21, 2024 05:46 |
|
I assume you implemented equals for Voter? I feel like you still don't understand how hashCode and HashMaps work, and would benefit from doing a little more reading. Your solution is not ideal
|
# ¿ Nov 15, 2022 13:50 |
|
samcarsten posted:Do you have any links to reading that may help? Searching the internet for documentation is an essential programming skill. That said, this article will likely give you some ideas: https://www.baeldung.com/java-hashmap
|
# ¿ Nov 15, 2022 14:22 |
|
Maybe it'd help to think about it like this, a hash map uses an algorithm, implemented within hashCode, to define an index for an object. So, when you insert an object into a map, it computes the index and inserts the entry at that index. Likewise, when you retrieve the object, it computes the index again to know where to find it. This is a gross simplification and not entirely accurate, but may help conceptualize what's happening. The problem with the original code was that it was not defining a hashCode and equals implementation, so Java was using the object's identity. Another simplification, but you could think of identity like as if Java assigned a unique number to every object created; different objects with the same content still have different identities. So, the broken code was trying to lookup by identity, which is almost never what you want, and was failing because the identities were different, even though the content of the objects was identical. By implementing hashCode and equals, you allow the map to base the "index" on the contents of the object rather than the identity. I'm sure your book has better explanations that you should revisit, but I hope that helps conceptualize. Understanding data structures is absolutely essential. Best of luck!
|
# ¿ Nov 17, 2022 14:11 |
|
Maybe try a few and see what suits you best? Personally, I use Intellij and highly recommend it. It has a ton of features, but I think some of the best are its static analysis and suggestions. It catches a lot of bugs that you might otherwise overlook. I hate to say it, but it's really clear when you open a file that was written by a developer in a different IDE, the average code quality and tidiness (mixed tabs and spaces in the same file! Ugh!) is notably worse. Not that IDE choice makes you a good or bad developer, but Intelllij offers features that make it easier to write better code. I would also recommend the SonarLint plugin.
|
# ¿ Dec 29, 2022 13:37 |
|
|
# ¿ May 21, 2024 05:46 |
|
Ihmemies posted:Qt, IntelliJ IDEA, Netbeans, Visual Studio just feel like "legacy" apps compared to VSCode. Ihmemies posted:Just because it's new and fancy is not a good enough reason. I can promise you that intellij is by far the best java ide. I currently work on a large legacy code base that has been worked on over the years using NetBeans, Eclipse, VSCode, and Intellij. Sadly, it is blindly obvious which contributions were not authored in intellij. But, you should totally use the editor that suits you best. There is a lot to be said for sticking with the editor you know and use for all of your other work rather than learning a new tool.
|
# ¿ Apr 29, 2023 15:54 |