RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Mike Simmons wrote:It looks like the place you're adding keys is here:
From this I can see that this "alphaList" is something you're adding and removing values from, and you're putting it in as key. This is a major problem. If you put a mutable object in a hashmap as a key, and you change the data in the mutable object afterwards, you confuse the heck out of the hashmap. Furthermore, if that code is run 100 times, your hashmap may look like it has 100 entries with 100 keys - but they aren't 100 different keys (even though they were supposed to be". They're 100 different references to the same single LinkedList object, which you keep changing as you go. That's no good. You need to create a new key object for each entry into the map. And don't change it afterwards.
One simple way to do this would be to call the LinkedList's clone() method. This makes a copy of the list with the same contents it had at the time you cloned it. If you want to keep using that alphaList, ok, but never put the alphaList itself into the map. Always put a clone() of the list into the map, and never change any data in the cloned key.
Mike Simmons wrote:One simple way to do this would be to call the LinkedList's clone() method. This makes a copy of the list with the same contents it had at the time you cloned it. If you want to keep using that alphaList, ok, but never put the alphaList itself into the map. Always put a clone() of the list into the map, and never change any data in the cloned key.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
I think OP was using a List<Integer>, so that problem won't arise; Integer is itself immutable.Rob Spoor wrote:. . . List.copyOf instead. It will return not just a copy, but more importantly an immutable copy. . . . . Its contents are still mutable . . .
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:You're right. I saw some List<int[][]>, and these arrays of arrays are mutable. The map keys are indeed List<Integer>, and then the result of List.copyOf will be fully immutable and therefore valid for map keys.
Sometimes the only way things ever got fixed is because people became uncomfortable.
Tim Holloway wrote:A Map contains a set of key/value pairs. The value can be null, but for a HashMap, the key cannot, and each key must be a distinct object, not "==" any other key object.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:Correct.
The use of null for a key or value depends on the implementation. For instance:
HashMap allows both TreeMap allows null values, but only allows null keys if a custom Comparator is used that supports null arguments ConcurrentHashMap, like Hashtable, supports neither
When you need null keys or values, always first look if the implementation you're thinking of supports it.
The Map.of, Map.ofEntries, and Map.copyOf static factory methods provide a convenient way to create unmodifiable maps. The Map instances created by these methods have the following characteristics:
...
They disallow null keys and values. Attempts to create them with null keys or values result in NullPointerException.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Mike Simmons wrote:I think we're all on the same page . . .
...Peace and quiet!we don't have a number, and therefore can't call him.
Campbell Ritchie wrote:...but the HashMap documentation allows a key to be null or to map to null. Agree it would have been better to ban nulls, as the old implementation did, and as Map#copyOf() and Map#of(...) do. Maybe allowing nulls is a mistake, and I was pleased to hear Jesse say that. Nasty case of violating the DRY principle.
People might come unstuck when they interpret getting null as meaning there isn't a mapping that hasn't been totally removed. That is what the containSxxx() methods look for. Maybe null is a valid value.
Maybe somebody used a Map as a phone book and before 1963 it might have contained “Campbell↦277”. [Neither phone number mentioned exists nowadays.]
From 1963-1966 it might have contained “Campbell↦null”. It will come as a surprise to many readers that in those days there were many people without phone numbers full stop. In those days we didn't havae a phone and the mapping to null recognises that absence. Permitting null keys is one way to implement that and it would be a way to record, “Campbell hasn't got a phone number.” It doesn't mean that getting null implies no record. On paper it might have read, “Campbell277”.
In 1966, that mapping would have changed to “Campbell↦64831”. Maybe maps had been invented in 1963. Maybe this is an argument for a value being null, but it would be better to use one of the techniques for avoiding nulls.
The old implementation prohibited nulls, which meant, rather than recording no value one had to delete the key and the whole Entry. Other implementations permit inactivating a mapping by putting null.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
No, it means, “No, we have lots of banana skins.”Jesse Silverman wrote:. . . "Yes, we have no bananas" . . .
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Campbell Ritchie wrote:Sir Tony Hoare has called the invention of null his million‑dollar mistake, his sixty‑fourꀑmillion‑dollar mistake, his billion‑dollar mistake, etc.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
He says that is true, and it is no joke; the number of programmers and the amount of code suffering problems caused by null increases year on year, and the total damage it does increases year on year, maybe exponentially.Jesse Silverman wrote:. . . the value of his mistake just keeps doubling every few years!
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
Jesse Silverman wrote:He's been around so long the value of his mistake just keeps doubling every few years!
Campbell Ritchie wrote:...but the HashMap documentation allows a key to be null or to map to null. Agree it would have been better to ban nulls, as the old implementation did, and as Map#copyOf() and Map#of(...) do. Maybe allowing nulls is a mistake, and I was pleased to hear Jesse say that. Nasty case of violating the DRY principle.
People might come unstuck when they interpret getting null as meaning there isn't a mapping that hasn't been totally removed. That is what the containSxxx() methods look for. Maybe null is a valid value.
...
The old implementation prohibited nulls, which meant, rather than recording no value one had to delete the key and the whole Entry. Other implementations permit inactivating a mapping by putting null.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |