Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashSet;

import java.util.Map;
import java.util.Set;

public class Main {
    /**
     * Returns set from a map referenced by the given key. If the set is not
     * present under the given key, a new list is created, inserted into the map
     * and returned. In other words, it ensures retrieval of a set for the given
     * key.
     *
     * @param <Key>   key type of the map
     * @param <Value> value type of the set (which acts as a value in the map)
     * @param map     key-value map, where value is of type {@link Set}
     * @param node    key
     * @return non-null {@link Set} for the given key
     */
    public static <Key, Value> Set<Value> getSet(Map<Key, Set<Value>> map, Key node) {
        Set<Value> set = map.get(node);
        if (set == null) {
            set = new HashSet<>();
            map.put(node, set);
        }
        return set;
    }
}