TMaps { @SuppressWarnings("unchecked") public static <E, A, V> void putResultDatasetOptimized( Map<E, Map<A, Set<V>>> map, E entity, Map<A, Set<V>> data) { if(map instanceof Insertable && map.getClass() != LazyTrackingTObjectResultDataset.class) { Insertable<E, A, V> dataset = (Insertable<E, A, V>) map; for (Entry<A, Set<V>> entry : data.entrySet()) { A attribute = entry.getKey(); for (V value : entry.getValue()) { dataset.insert(entity, attribute, value); } } } else { map.put(entity, data); } } private  TMaps(); static SortedMap<K, V> asSortedMap(
            Map<K, V> map); static Set<K> extractKeysFromEntrySet(
            Collection<Entry<K, V>> entrySet); static Collection<V> extractValuesFromEntrySet(
            Collection<Entry<K, V>> entrySet); static Map<K, V> fromEntrySet(
            Collection<Entry<K, V>> entrySet); static Map<K, V> newLinkedHashMapWithCapacity(int capacity); static V putIfAbsent(Map<K, V> map, K key, V value); @SuppressWarnings("unchecked") static void putResultDatasetOptimized(
            Map<E, Map<A, Set<V>>> map, E entity, Map<A, Set<V>> data); static V supplyIfAbsent(Map<K, V> map, K key,
            Supplier<V> supplier);  }
@Test public void testTObjectResultDatasetPutPerformance() throws InterruptedException { Map<Long, Map<String, Set<TObject>>> spec = Maps.newLinkedHashMap(); int rounds = 1000; TimeUnit unit = TimeUnit.MICROSECONDS; for (int i = 0; i < rounds; ++i) { String key = Random.getSimpleString(); Set<TObject> values = Sets.newLinkedHashSet(); for (int j = 0; j < 10; ++j) { values.add(Convert.javaToThrift(Random.getObject())); } Map<String, Set<TObject>> entry = Maps .newLinkedHashMapWithExpectedSize(1); entry.put(key, values); spec.put((long) i, entry); } Map<Long, Map<String, Set<TObject>>> map = Maps.newLinkedHashMap(); Map<Long, Map<String, Set<TObject>>> dataset = new LazyTrackingTObjectResultDataset(); Benchmark mapBench = new Benchmark(unit) { @Override public void action() { spec.forEach((record, data) -> { TMaps.putResultDatasetOptimized(map, record, data); }); } }; Benchmark datasetBench = new Benchmark(unit) { @Override public void action() { spec.forEach((record, data) -> { TMaps.putResultDatasetOptimized(dataset, record, data); }); } }; AtomicLong datasetTime = new AtomicLong(0); AtomicLong mapTime = new AtomicLong(0); CountDownLatch latch = new CountDownLatch(2); Thread t1 = new Thread(() -> { datasetTime.set(datasetBench.run()); latch.countDown(); }); Thread t2 = new Thread(() -> { mapTime.set(mapBench.run()); latch.countDown(); }); t2.start(); t1.start(); latch.await(); double datasetTimeDigits = Math.ceil(Math.log10(datasetTime.get())); double mapTimeDigits = Math.ceil(Math.log10(mapTime.get())); System.out.println(AnyStrings.format("Dataset = {} {} with {} digits", datasetTime.get(), unit, datasetTimeDigits)); System.out.println(AnyStrings.format("Map = {} {} with {} digits", mapTime.get(), unit, mapTimeDigits)); Assert.assertTrue( AnyStrings.format("Datset took {} {} and Map took {} {}", datasetTime.get(), unit, mapTime.get(), unit), datasetTimeDigits - mapTimeDigits <= 1); }