1

これは最良のデータ構造ではないかもしれませんが、これが可能かどうか疑問に思っていました.私はツールのセットを持っており、それぞれが一意の ID と属性を持っています. 各ツールには、属性を含むチャンバーのコレクションもあります。このツールを HashMap のキーとして使用し、Chambers のリストを値として使用したいと考えていました。
データベースからすべてのチャンバー情報を取得したら、各チャンバーを適切なツールに追加できるように、toolId でキー オブジェクト (ツール) を取得します。toolId を使用するために equals メソッドと hash メソッドをオーバーライドしました。

すべてのキーを元に戻し、それらを繰り返し処理してツール ID と等しいかどうかを確認する以外に、キー オブジェクトを取得する方法はありますか

これまでの私のコードは次のとおりです。

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 ( LinkedHashMap )を持つ ToolBean を使用して構造を作成し、ツールを開いて新しいチャンバーをマップに追加し、ツールを元のマップに戻すことができることを知っています。そのステップをスキップする方法があるかどうか疑問に思っていました。

ありがとう、ブリタ

4

2 に答える 2

1

仮定

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 値の処理は、演習として残されています。

于 2013-08-19T19:50:52.317 に答える