Search code examples
hashmapputjsonlinkedhashmap

Map put not working Java


I have a Map with some values, and I need to put a new entry into it, the problem is that the put "lost" the Object, and I have only the new Key into it.

Here is the code

public void addSeries(SerieInterface serieObject)
{
    Map<String, Object> series = this.getSeries();
    if (series == null)
        series = new HashMap<String, Object>();
    series.put(serieObject.getSerie_instance(), serieObject);
    this.setSeries(series);
} 

"series" is the existent map (is a BasicDBObject, it extendes LinkedHashMap) and I need to add in it the serieObject object (an Object with two String field that extends JSONObject); after the "put" command, I have the old two Object in the map and the new with only the "key" (i have only {"Key" : { } } ) (I have checked serieObject in debug, it have all the fields)

Any suggestion of the cause? Why the Object "Value" of the Map is lost? I have not any type of Exception from it, I only lost the value...

Thanks for the help, sorry for my not-perfect English


Solution

  • Luca, please describe more clearly what you have in your structure at the end of your test, {"Key" : { } } doesn't mean much, what kind of Object is the {} value?

    Looking at this series.put(serieObject.getSerie_instance(), serieObject); serieObject seems to have at least that weirdly named getSerie_instance key and it's not null.

    Also, a few other things:

    1) this.getSeries() , if this Map is a class property use that directly, don't use setter/getter if you only return the value.

    2) if (series == null), why not initialize this Map in the constructor of the class? It's cleaner this way.