Java tutorial
//package com.java2s; /****************************************************************************** * e-voting system * * Copyright (C) 2016 DSX Technologies Limited. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied * * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * See the GNU General Public License for more details. * * * * You can find copy of the GNU General Public License in LICENSE.txt file * * at the top-level directory of this distribution. * * * * Removal or modification of this copyright notice is prohibited. * * * ******************************************************************************/ import java.util.Map; import java.util.function.Supplier; public class Main { public static <K, V> V getOrAdd(Map<K, V> map, K key, Supplier<V> constructor) { V value = map.get(key); if (value == null) { value = constructor.get(); map.put(key, value); } return value; } }