java - How can I use a HashMap as a way to access and copy items from a list? -
i working on videogame friend. account different types of items, had class each item extending item class. there wasn't data in these classes, looking alternative our workspace wasn't cluttered. started learning hashmaps, , thought awesome way add items. set instead of accessing items in hashmap int, make arraylist, access them strings. started adding functionality, creating anonymous items in item class,
private static item coal = new item() { weight = .2; setimageid(0, 16); }
and adding them hashmap.
itemmap.put("coal", coal);
after doing few of these, realized there 1 item of each type in list, , if ever wanted have multiples of items modified without modifying original, need make copies. started doing research on how that. use copy constructor, there many variables in item done efficiently. that, wondering if there simple solution. make of items final? i'm spitballing, because totally new area of programming. doing whole thing wrong, too. need way use hashmap create of "item database" can use access indefinite amount of item in list. suggestions?
how having hashmap has value of set (or list depending on if same item can exist more once)?
map<string, set<item>> map = new hashmap<string, set<item>>();
that way, can have multiple items each type.
one idiom adding new item type's set below:
set<item> items = map.get(type); if (items == null) { items = new hashset<item>(); items.put(type, items); } items.add(item);
it might not bad idea use enum
type instead of string. map.put(item.coal, itemset);
guard against typos , case sensitivity issues.
Comments
Post a Comment