hashmap - Java retrieving a custom key object from a map -
this may not best data structure, wondering if possible this: have set of tools, each unique id , bunch or attributes. each tool has collection of chambers contain attributes. hoping use tool key hashmap , list of chambers value.
after of chamber information database, want key object (tool) toolid can add each chamber appropriate tool. over-rode equals method , hash method use toolid.
other bringing keys , iterating on them see if equal toolid, there way key object
here code far:
public class toolbean { private string toolid; private string toolname; private string toolowner; public toolbean(string toolid){ this.toolid = toolid; } @override public boolean equals(object obj) { if (this == obj) return true; if (obj == null) return false; if (getclass() != obj.getclass()) return false; toolbean other = (toolbean) obj; if (toolid == null) { if (other.toolid != null) return false; } else if (!toolid.equals(other.toolid)) return false; return true; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((toolid == null) ? 0 : toolid.hashcode()); return result; } }
the structure creating this:
linkedhashmap<toolbean, linkedhashmap<string, chamberbean>> toolwithchambermap = new linkedhashmap<toolbean, linkedhashmap<string, chamberbean>>();
i know can create structure toolbean having linkedhashmap of chambers (linkedhashmap ) open tool, add new chamber map, put tool in original map. wondering if there way skip step.
thanks, brita
assuming
class toolbean { // described in op } class chamber { // opaque class }
what seem asking this:
// master map of toolbean map of chamber objects map<toolbean, map<string, chamber>> toolbeantochambermap = new linkedhashmap<toolbean,map<string,chamber>>(); // tool bean , chamber toolbean tb1 = new toolbean(...); chamber ch1 = new chamber(...); // create map contain chambers , string keys map<string,chamber> chmap = new linkedhashmap<string,chamber>(); // put chamber map chmap.put("one",ch1); // put map of chambers master map, keyed off toolbean toolbeantochambermap.put(tb1, chmap); // sometime later ... toolbean tb2 = ... // may same tb1 // new chamber added data structure chamber ch2 = new chamber(...); // first find chamber map in master map, matching toolbean of interest map<string,chamber> temp = toolbeantochambermap.get(tb2); // 'temp' reference submap - if it's null, toolbean wasn't in master map yet if (temp == null) { // create new empty submap temp = new linkedhashmap<string,chamber>(); // add master map toolbeantochambermap.put(tb2,temp); } // @ point 'temp' either pre-existing submap or 1 added temp.put("two",ch2);
however, unless have reason doing things way, i'd suggest following:
public class toolbean { attributes... map<string, chamber> chambermap = new linkedhashmap<string,chamber>(); ... public void addchamber(string name, chamber c) { // similar logic above } public chamber getchamber(string name) { return chambermap.get(name); } } set<toolbean> toolbeans = new hashset<toolbean>(); toolbean tb1 = new toolbean(); tb1.addchamber("one", new chamber(...)); tb1.addchamber("two", new chamber(...)); toolbeans.add(tb1);
in other words, hide complexity of map of chambers inside toolbean
class.
handling of duplicate chamber values, , null values, left exercise.
Comments
Post a Comment