performance - Java: Add element into a list inside a HashMap -


I was thinking that there is a more effective way of adding an element than adding a list inside a constraints list To add an element to the copy, and then insert it inside the map with a specific location inside the map, to overwrite the location of that map portion ...

  Maps & lt; Integer, list & lieutenant; Object & gt; & Gt; MyMap = new Hashmap & lt; Integer, list & lt; Object & gt; & Gt; (); .... Add function list & lt; Object & gt; Tmp = new linked list (); If (myMap.containsKey (myListKey.hashCode ())) {// kcheck TMP = myMap.get (myListKey.hashCode ()); // copy tmp.add (myObjectToAdd); // add myMap.put (myListKey.hashCode (), TMP); // Overwrite}  

I tried several ways to get direct access to the internal list, but the compiler seems to be in his / her time today ...

First of all, there is no reason why you call hash code () to enter the item in the list should do. For one thing, different keys may have the same hash code, as a result you probably will not behave. Just use the key as the key

> Maps & lt; List insect type, list & lt; Object & gt; & Gt; MyMap = ... ... Add a list of functions & lt; Object & gt; Tmp = myMap.get (myListKey); If (tmp == tap) {tmp = new linked list & lt; Object & gt; (); MyMap.put (myListKey, tmp); } Tmp.add (myObjectToAdd);

In this way, you only create a new list when there is no key in the map.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -