繁体   English   中英

Java从地图检索自定义键对象

[英]Java retrieving a custom key object from a map

这可能不是最好的数据结构,但是我想知道是否可以做到这一点:我有一组工具,每个工具都有唯一的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创建一个结构,然后打开该工具,将新房间添加到地图中,然后将工具放回原始地图中。 我想知道是否有办法跳过这一步。

谢谢,Brita

我个人将创建2个地图:ToolId-> ToolBean和ToolId-> Chambers。 它与您的方法类似,但是我不喜欢ToolBean是地图中的关键。 我看不到ToolBean作为键有什么好处,而且它还可以编译代码,因为您需要重写等于和忽略名称和所有者的hashcode方法。

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

第三种使用代码的方法是:拥有ToolId,您可以使用此ID创建ToolBean实例,并将其用作检索房间图的键。 就个人而言,这对我来说就像是骇客。

假设

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值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM