这可能不是最佳的数据结构,但是我想知道是否可以这样做:
我有一组工具,每个工具都有一个唯一的ID和一堆或属性。每个工具还具有包含属性的腔室集合。
我希望使用该工具作为HashMap的关键字,并使用Chambers of Chambers作为值。
从数据库中获取所有腔室信息后,我想通过toolId获取关键对象(工具),以便可以将每个腔室添加到其适当的工具中。我重写了equals方法和hash方法以使用toolId。

除了带回所有键并遍历它们以查看它们是否等于toolId之外,还有其他方法可以获取键对象

到目前为止,这是我的代码:

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;
    }
}


我正在创建的结构如下所示:

LinkedHashMap<ToolBean, LinkedHashMap<String, ChamberBean>> toolWithChamberMap =  new LinkedHashMap<ToolBean, LinkedHashMap<String, ChamberBean>>();


我知道我可以使用具有Chambers的LinkedHashMap(LinkedHashMap)的ToolBean创建一个结构,然后打开该工具,将新的Chamber添加到地图中,然后将工具放回原始地图中。我想知道是否有办法跳过这一步。

谢谢,
布里塔

最佳答案

假设

class ToolBean {
    // as described in OP
}

class Chamber {
    // some opaque class
}


您似乎想要的是:

// Master map of ToolBean to map of Chamber objects
Map<ToolBean, Map<String, Chamber>> toolBeanToChamberMap =
    new LinkedHashMap<ToolBean,Map<String,Chamber>>();

// A tool bean and a chamber
ToolBean tb1 = new ToolBean(...);
Chamber  ch1 = new Chamber(...);

// Create a map that will contain Chambers and their String keys
Map<String,Chamber> chMap = new LinkedHashMap<String,Chamber>();

// Put the Chamber into this map
chMap.put("one",ch1);

// Put the map of Chambers into the master map, keyed off the ToolBean
toolBeanToChamberMap.put(tb1, chMap);

// sometime later ...

ToolBean tb2 = ... // may be the same as tb1

// A new Chamber to be added to the data structure
Chamber ch2 = new Chamber(...);

// First find the Chamber map in the master map, matching the ToolBean of interest
Map<String,Chamber> temp = toolBeanToChamberMap.get(tb2);

// 'temp' is a reference to the submap - if it's null, this ToolBean wasn't in the master map yet
if (temp == null) {
    // So create a new empty submap
    temp = new LinkedHashMap<String,Chamber>();
    // Add it to the master map
    toolBeanToChamberMap.put(tb2,temp);
}
// At this point 'temp' is either the pre-existing submap or the one we just added
temp.put("two",ch2);


但是,除非您有充分的理由这样做,否则我建议以下几点:

public class ToolBean {
    some attributes...
    Map<String, Chamber> chamberMap = new LinkedHashmap<String,Chamber>();
    ...
    public void addChamber(String name, Chamber c) {
        // similar logic as 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);


换句话说,将所有密室图的复杂性隐藏在ToolBean类内部。

作为练习,处理重复的Chamber值和null值。

07-27 16:21