List of usage examples for java.util Collections newSetFromMap
public static <E> Set<E> newSetFromMap(Map<E, Boolean> map)
From source file:Main.java
public static void main(String args[]) { Map<String, Boolean> map = new WeakHashMap<String, Boolean>(); // create a set from map Set<String> set = Collections.newSetFromMap(map); // add values in set set.add("Java"); set.add("C"); set.add("C++"); set.add("from java2s.com"); // set and map values are System.out.println("Set is: " + set); System.out.println("Map is: " + map); }
From source file:Main.java
public static <T> Set<T> newConcurentHashSet() { return Collections.newSetFromMap(new ConcurrentHashMap<T, Boolean>()); }
From source file:Main.java
public static <T> Set<T> newConcurentHashSet(int initialCapacity) { return Collections.newSetFromMap(new ConcurrentHashMap<T, Boolean>(initialCapacity)); }
From source file:Main.java
/** * There is currently no class in Java for a weak set, but it can be created as a set wrapping a weak map as in the body of the method below. * @return a weak set//from w w w . j a v a 2s . com */ public static <T> Set<T> createWeakSet() { return Collections.newSetFromMap(new WeakHashMap<T, Boolean>()); }
From source file:Main.java
/** * Create a new identityHashSet.//from w w w . j ava2 s . co m * @return */ public static <E> Set<E> identityHashSet() { return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>()); }
From source file:Main.java
/** * Returns a set backed by the specified map. The resulting set displays * the same ordering, concurrency, and performance characteristics as the * backing map. In essence, this factory method provides a {@link Set} * implementation corresponding to any {@link Map} implementation. There * is no need to use this method on a {@link Map} implementation that * already has a corresponding {@link Set} implementation (such as {@link * HashMap} or {@link TreeMap})./*from w ww . j a v a 2 s .c o m*/ * * <p>Each method invocation on the set returned by this method results in * exactly one method invocation on the backing map or its <tt>keySet</tt> * view, with one exception. The <tt>addAll</tt> method is implemented * as a sequence of <tt>put</tt> invocations on the backing map. * * <p>The specified map must be empty at the time this method is invoked, * and should not be accessed directly after this method returns. These * conditions are ensured if the map is created empty, passed directly * to this method, and no reference to the map is retained, as illustrated * in the following code fragment: * <pre> * Set<Object> weakHashSet = Collections.newSetFromMap( * new WeakHashMap<Object, Boolean>()); * </pre> * * @param map the backing map * @return the set backed by the map * @throws IllegalArgumentException if <tt>map</tt> is not empty */ public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) { return Collections.newSetFromMap(map); }
From source file:com.ndemyanovskyi.observing.Listeners.java
Set<L> softSet() { if (softSet == null) { softSet = Collections.newSetFromMap(new ReferenceMap<>(SOFT, HARD)); } return softSet; }
From source file:org.apache.stratos.load.balancer.common.domain.Cluster.java
public Cluster(String serviceName, String clusterId) { this.serviceName = serviceName; this.clusterId = clusterId; this.hostNames = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); this.memberMap = new HashMap<String, Member>(); this.hostNameToContextPathMap = new ConcurrentHashMap<String, String>(); }
From source file:com.ndemyanovskyi.observing.Listeners.java
Set<L> weakSet() { if (weakSet == null) { weakSet = Collections.newSetFromMap(new ReferenceMap<>(WEAK, HARD)); } return weakSet; }
From source file:com.blacklocus.qs.worker.util.log.SamplingQSLogServiceTest.java
@Test public void testSampledLifeCycle() throws InterruptedException { // With these parameters, by far most logger interactions should be filtered out, very few sampled in. final int numThreads = 64, iterations = 200, processingJitterMaxMs = 16, noSoonerThanMs = 100; final Set<String> sampledTaskIds = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); final QSLogService logService = Mockito.mock(QSLogService.class); // track which logging interactions were allowed through (sampled in) Mockito.doAnswer(new Answer() { @Override/* ww w .j av a 2 s .c o m*/ public Object answer(InvocationOnMock invocation) throws Throwable { sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId); return null; } }).when(logService).startedTask(Matchers.any(QSTaskModel.class)); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { sampledTaskIds.add(((QSLogModel) invocation.getArguments()[0]).taskId); return null; //TODO jason } }).when(logService).log(Matchers.any(QSLogModel.class)); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { sampledTaskIds.add(((QSTaskModel) invocation.getArguments()[0]).taskId); return null; //TODO jason } }).when(logService).completedTask(Matchers.any(QSTaskModel.class)); Predicate<QSTaskModel> taskPredicate = SamplingPredicates.noSoonerThan(noSoonerThanMs, TimeUnit.MILLISECONDS); final QSLogService sampledLogService = new SamplingQSLogService(logService, taskPredicate); long startNs = System.nanoTime(); ExecutorService threads = Executors.newFixedThreadPool(numThreads); for (int i = 0; i < numThreads; i++) { threads.submit(new Callable<Void>() { @Override public Void call() throws Exception { LOG.debug("Thread start {}", Thread.currentThread().getName()); for (int i = 0; i < iterations; i++) { String taskId = UUID.randomUUID().toString(); // simulate task processing, some have logs, some don't, processing time varies between each step QSTaskModel task = new QSTaskModel(); task.taskId = taskId; Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs)); sampledLogService.startedTask(task); Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs)); // random number of associated logs [0, 2] for (int j = RandomUtils.nextInt(2); j > 0; j--) { QSLogModel log = new QSLogModel(); log.taskId = taskId; sampledLogService.log(log); Thread.sleep(RandomUtils.nextInt(processingJitterMaxMs)); } sampledLogService.completedTask(task); } LOG.debug("Thread end {}", Thread.currentThread().getName()); return null; } }); } threads.shutdown(); threads.awaitTermination(1, TimeUnit.MINUTES); long endNs = System.nanoTime(); // Theoretical maximum number of sampled in task logging long durationMs = TimeUnit.NANOSECONDS.toMillis(endNs - startNs); long expectedMax = durationMs / noSoonerThanMs + 1; // +1 for time@0: sampled in LOG.debug("Run duration: {}ms no sooner than: {}ms", durationMs, noSoonerThanMs); LOG.debug("Expected max sampled in: {} Actually sampled: {}", expectedMax, sampledTaskIds.size()); Assert.assertTrue(expectedMax >= sampledTaskIds.size()); }