문제

This may not be the best data structure, but I was wondering if it is possible to do this: I have a set of tools, each with a unique Id and a bunch or attributes. Each tool also has a collection of chambers which contain attributes. I was hoping to use the tool as the key to a HashMap and the List of Chambers as the value.
After I get back all of the chamber information from the database, I want to get the key object (tool) by toolId so that I can add each chamber to its appropriate tool. I over-rode the equals method and hash method to use the toolId.

Other than bringing back all the keys and iterating over them to see if they equal the toolId, is there some way to get the key object

Here is my code so 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 I am creating will look like this:

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

I know that I can create a structure with the ToolBean having an LinkedHashMap of Chambers (LinkedHashMap ) then open the tool, add the new chamber to the map, the put the tool back in the original map. I was wondering if there was a way to skip that step.

Thanks, Brita

도움이 되었습니까?

해결책

Assuming

class ToolBean { 
    // as described in OP
}

class Chamber {
    // some opaque class
}

What you seem to be asking for is this:

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

However, unless you have a really good reason for doing things this way, I'd suggest the following:

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

In other words, hide all the complexity of the map of chambers inside the ToolBean class.

Handling of duplicate Chamber values, and null values, left as an exercise.

다른 팁

Personally I'd create 2 maps: ToolId -> ToolBean and ToolId -> Chambers. It similar to your approach but I don't like ToolBean being key in the map. I don't see much benefits of ToolBean as keys and also it compilcates code because you need to override equals and hashcode methods that ignore name and owner.

Second way is to embed chambers to ToolBean itself.

Third way that uses your code is this: having ToolId you can create instance of ToolBean using this id and use it as key to retrieve map of chambers. Personally it feels like hack to me.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top