map - Java HashMap only returning last element of list -


i'm working on 2d game, , i'm using lwjgl , slick-util library help. there's nothing complicated , i've programmed more difficult things before. however, i've encountered odd problem hashmap / map return values. code regarding hashmap:

    public map<string, texture> textures;      public void loadtextures()     {         textures = new hashmap<string, texture>();          textures.put("char_up", main.loadtexturefrom("character/character_up.png"));         textures.put("char_down", main.loadtexturefrom("character/character_down.png"));         textures.put("char_right", main.loadtexturefrom("character/character_right.png"));         textures.put("char_left", main.loadtexturefrom("character/character_left.png"));     } 

however, anytime use textures.get(framehere), returns last item put hashmap, if last item "char_right", return char_right texture, if called textures.get("char_up"). tried setting character positions loading instance of texture every time character changed directions, , worked perfectly. meaning, problem isn't texture loader or way i'm getting textures value hashmap.

this pretty frustrating , i'm @ huge loss might messing here. maybe there incredibly simple i'm missing, because i've done in java before without problem, comparison looks 100% fine. appreciated. thanks.

edit:

here loadtexturefrom() method:

public texture loadtexturefrom(string path) {     try     {         return textureloader.gettexture("png", new fileinputstream(new file("res/textures/" + path)));     }     catch (filenotfoundexception e)     {         e.printstacktrace();     }     catch (ioexception e)     {         e.printstacktrace();     }      return null; } 

a common misconception collections including hashmap, when adding or putting key/value/element placing copy of element. instead placing copy of reference element.

the problem describe possible if main.loadtexturefrom returning same image each time. work if used image before calling method again, if want save image, need save copy of it.


Comments