Basically you need to override hashCode() and equals() in place Presumably Location already overrides these methods.
Basically you need to override hashCode() and equals() in place. Presumably Location already overrides these methods. Those are the methods that HashMap uses to first narrow down the list of candidate keys very quickly (using the hash code) and then check them for equality (by calling equals).
It's not clear what the serializable problem is - my guess is that although place is serializable, Location isn't. If you could post a short but complete problem demonstrating the problem, that would really help. (It would also be a good idea to start following Java naming conventions, and making your fields private...) EDIT: Here's an example of the Place class with hash code and equality.
Note that I've made it immutable for the sake of avoiding the values changing after it's used as a key in a hash map - I don't know offhand how well that works with serialization, but hopefully it's okay: public final class Place implements Serializable { private static final long serialVersionUID = -8851896330953573877L; private final String world; // Do you definitely want Double here rather than double? Private final Double x; private final Double y; private final Double z; public Place(String world, Double x, Double y, Double z) { this. World = world; this.
X = x; this. Y = y; this. Z = z; } @Override public int hashCode() { int hash = 17; hash = hash * 31 + (world == null?0 : world.hashCode()); hash = hash * 31 + (x == null?0 : x.hashCode()); hash = hash * 31 + (y == null?0 : y.hashCode()); hash = hash * 31 + (z == null?0 : z.hashCode()); return hash; } @Override public boolean equals(Object other) { if (!(other instanceof Place)) { return false; } Place p = (Place) other; // Consider using Guava's "Objects" class to make this simpler return equalsHelper(world, p.
World) && equalsHelper(x, p. X) && equalsHelper(y, p. Y) && equalsHelper(z, p.
Z); } private static boolean equalsHelper(Object a, Object b) { if (a == b) { return true; } if (a == null || be == null) { return false; } return a. Equals(b); } // TODO: Add getters? } It's worth noting that this will be comparing Double values for equality, which is almost always a bad idea... but you can't really give a tolerance in something like equals.So long as the values are exactly the same when you come to look them up, it should work fine.
The public/private cleanup would be done in a later stage when we plan to start using the code in a beta form. I'll see what I can do about creating a... smaller... project to display the exact complications as it fits in the code. - can you show me an example of the override process you were talking about?
I've read up about it once or twice but wasn't sure how to use it? – James Aug 31 at 22:59 1 @James: It's really a much better to get simple things like variable access right straight away. Will edit with an example of hashing for Place... – Jon Skeet Aug 31 at 23:04 MANY thanks... very insightful.
Been coding c++ so long, jumping into java has been a bit of a learning curve. The code you listed has a few flaws, like a null long would always == 0, making it not null, so I defaulted it to give the hashCode() to remove the error I was getting. Also, mistype "return equalsHeler(world, p.
World) &&". Think I get how serializable should be implemented now :P and the overrides will come in handy. My code now works without errors ^.
^ guess I can clean it up now! I was afraid I was going to have to save it to a SQL database for a second there >. > – James Aug 31 at 23:51 @James: What do you mean by "a null long would always == 0"?
There aren't any longs in there. But it's find for the hash to be 0. No single field can make the hash 0 though... – Jon Skeet Aug 31 at 23:59 the 3 "hash = hash..." lines working on Doubles errors with "The operator == is undefined for the argument type(s) double, null", defaulted them to "hash = z.hashCode();" might be unsafe?
If you know a better way, your welcome to lemme know – James Aug 317 at 0:15.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.