Java从地图中检索自定义密钥对象

时间:2013-08-19 19:43:36

标签: java hashmap linkedhashmap

这可能不是最好的数据结构,但我想知道是否可以这样做: 我有一套工具,每个工具都有一个唯一的Id和一堆或属性。每个工具还有一组包含属性的腔室。 我希望使用该工具作为HashMap和Chambers列表的键作为值 在我从数据库中获取所有腔室信息后,我想通过toolId获取关键对象(工具),以便我可以将每个腔室添加到其适当的工具中。我过度使用equals方法和哈希方法来使用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>>();

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

谢谢, 布丽塔

2 个答案:

答案 0 :(得分:1)

我个人创建了2张地图:ToolId - &gt; ToolBean和ToolId - &gt;钱伯斯。它与您的方法类似,但我不喜欢ToolBean是地图中的关键。我没有看到ToolBean作为键的好处,也没有编译代码,因为你需要覆盖忽略name和owner的equals和hashcode方法。

第二种方法是将腔室嵌入ToolBean本身。

使用你的代码的第三种方式是这样的:拥有ToolId你可以使用这个id创建ToolBean的实例并使用它作为密钥来检索腔室的地图。就个人而言,这对我来说就像是黑客。

答案 1 :(得分:0)

假设

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值和空值,作为练习。