我有一张应该包含从整数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