Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Returns list from a map referenced by the given key. If the list 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 list for the
     * given key.
     *
     * @param <Key>   key type of the map
     * @param <Value> value type of the list (which acts as a value in the map)
     * @param map     key-value map, where value is of type {@link List}
     * @param node    key
     * @return non-null {@link List} for the given key
     */
    public static <Key, Value> List<Value> getList(Map<Key, List<Value>> map, Key node) {
        List<Value> list = map.get(node);
        if (list == null) {
            list = new ArrayList<>();
            map.put(node, list);
        }
        return list;
    }
}