Java 从 map 中检索自定义键对象

标签 java hashmap linkedhashmap

这可能不是最好的数据结构,但我想知道是否可以这样做: 我有一套工具,每个工具都有一个唯一的 ID 和一堆属性。每个工具还具有一组包含属性的室。 我希望使用该工具作为 HashMap 的键,并将 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>>();

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

谢谢, 布丽塔

最佳答案

假设

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

换句话说,将房间 map 的所有复杂性隐藏在 ToolBean 类中。

处理重复的 Chamber 值和空值,留作练习。

关于Java 从 map 中检索自定义键对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18321843/

相关文章:

java - HashMap用于存储大Cache

java - 将字符串数组列表拆分为多个子字符串并将它们添加到 hashmap : JAva | Selenium

java - LRU缓存的LinkedHashMap结构

java - 我更改了 Web 服务声明,然后 wsimport 说我有一条重复消息

java - Java下播放*.caf文件(iOS/Apple)

java - 从另一个 Maven 项目加载消息属性

Java + Postgresql : How to store Parent and List of all children in a HashMap<String, ArrayList <字符串>>

java - 排序 LinkedHashMap

java - 如何将一个 HashMap 中的值与另一个 HashMap 中的相同值进行比较?

java - 使整个 EditText 可见?