Here you can find the source of getNestedSet(K1 key, ConcurrentMap
Parameter | Description |
---|---|
key | the key to the concurrent map |
set | the concurrent map |
public static <K1, K2> Set<K2> getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set)
//package com.java2s; /*// w w w. j a v a 2s . c om * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ import java.util.Collections; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; public class Main { /** * Get and/or allocate as needed a nested concurrent set inside a concurrent * map in a threadsafe way. * @param key the key to the concurrent map * @param set the concurrent map * @return the nested concurrent set */ public static <K1, K2> Set<K2> getNestedSet(K1 key, ConcurrentMap<K1, Set<K2>> set) { Set<K2> inner = set.get(key); if (inner == null) { inner = Collections.newSetFromMap(new ConcurrentHashMap<K2, Boolean>()); Set<K2> old = set.putIfAbsent(key, inner); if (old != null) inner = old; } return inner; } }