Ken
Ken

Reputation: 343

Creating a new Set everytime a method is called

I have a map that should contain relations from an integer a to an integer b. Integer b should be in a Set. Relations from integer a to integer b can be added using the add method. To create such a relation, I have to create a new Set (to contain b) everytime the add method is called. How should I do this? I think I know how to do this with arrays since they support names containing variables, but sets don't.

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

Upvotes: 0

Views: 161

Answers (3)

fabian
fabian

Reputation: 82491

Just add a new Set, if there isn't already a mapping for a in the Map and then add the value to the Set in the Map (whether it was just added or previously in the map):

connection.computeIfAbsent(a, k -> new HashSet<Integer>()).add(b);

Upvotes: 3

Eric G
Eric G

Reputation: 926

I think this is what you're looking for

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 key, int val) {
        if(!connection.containsKey(key)){
            connection.put(key, new HashSet<>());    
        }            
        connection.get(key).add(val);
    }
    ...
}

!connection.containsKey(key) will check if the HashMap contains the key mapping. If it doesnt, it will add an mapped entry for {key, HashSet} where HashSet is an empty HashSet<Integer>

connection.get(key) will return the HashSet<Integer> associated to the key in the HashMap.

.add(val) will now add the value to the HashSet<Integer>

This guarantees that a hashset is created if the key doesnt exist, and then it adds the value to the set owned by the key

Upvotes: 2

khelwood
khelwood

Reputation: 59185

Suppose you call add(4,5) and then add(4,6).
If the result is that your map now contains 4 -> {5,6} (that is, the key 4 links to a set containing 5 and 6), then what you are making is a multi-valued map.

A way to add into a multi-valued map is something like this:

public void add(int a, int b) {
    Set<Integer> values = connection.get(a);
    if (values==null) {
        values = new HashSet<Integer>();
        connection.put(a, values);
    }
    values.add(b);
}

That is, get the set associated with the key a.
If there isn't one, create one and add it to the map.
Add your value b to the set.

Upvotes: 3

Related Questions