我有一張應該包含從整數a到整數b的關係的映射。整數b應該在一個集合中。從整數a到整數b的關係可以使用add方法添加。要創建這樣的關係,每次調用add方法時都必須創建一個新的Set(包含b)。我應該怎麼做?我想我知道如何對數組做這個操作,因爲它們支持包含變量的名稱,但是不能。每次調用方法時創建一個新的集合
public class intRelImplementation extends intRel {
protected final Map<Integer, Set<Integer>> connection;
public intRelImplementation (final int n) {
super(n);
connection = new HashMap<>();
}
@Override
public void add(int a, int b) {
// I have to create a new Set everytime the Add method is called.
// The Set should contain the Integer b, and this set should then be
// placed into the Map: Map<a, Set<b>>.
Set<Integer> setInMap = new HashSet<>(); //not correct obviously
Set setInMap2 = new HashSet(setInMap);
}
擁有一套哪些線路在'add'應該是在做什麼?您是否試圖將鍵/值對添加到您的多值圖中? – khelwood
@khelwood如果使用多值映射表示包含多個映射的映射,那麼是的。例如,有效的命令可以在之後添加(4,5)和添加(7,8)。然後,該映射應該包含從整數4到包含整數5的集合的映射,以及從整數7到包含整數8的集合的映射。 – Ken
好吧,但是如果您執行'add(4,5)'和然後'添加(4,6)',那麼你期望什麼? – khelwood