Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
moosferatu
Jan 29, 2020
Is this some kind of no-google assignment? You should be able to easily search for how to implement hashCode and equals in Java

Adbot
ADBOT LOVES YOU

moosferatu
Jan 29, 2020
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

moosferatu
Jan 29, 2020

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

moosferatu
Jan 29, 2020
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!

moosferatu
Jan 29, 2020
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.

Adbot
ADBOT LOVES YOU

moosferatu
Jan 29, 2020

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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply