/** * Compact the {@link Map} backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { @DoNotSub final int idealCapacity = (int)Math.round(size() * (1.0d / loadFactor)); rehash(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, idealCapacity))); }
@Test public void shouldClearCollection() { for (int i = 0; i < 15; i++) { intToObjectMap.put(i, Integer.toString(i)); } assertThat(intToObjectMap.size(), is(15)); assertThat(intToObjectMap.get(1), is("1")); intToObjectMap.clear(); assertThat(intToObjectMap.size(), is(0)); Assert.assertNull(intToObjectMap.get(1)); }
@Test public void shouldIterateAndHandleRemove() { final Collection<Integer> initialSet = new HashSet<>(); final int count = 11; for (int i = 0; i < count; i++) { final String value = Integer.toString(i); intToObjectMap.put(i, value); initialSet.add(i); } final Collection<Integer> copyOfSet = new HashSet<>(); int i = 0; for (final Iterator<Integer> iter = intToObjectMap.keySet().iterator(); iter.hasNext(); ) { final Integer item = iter.next(); if (i++ == 7) { iter.remove(); } else { copyOfSet.add(item); } } final int reducedSetSize = count - 1; assertThat(initialSet.size(), is(count)); assertThat(intToObjectMap.size(), is(reducedSetSize)); assertThat(copyOfSet.size(), is(reducedSetSize)); }
@Test public void shouldReplaceExistingValueForTheSameKey() { final int key = 7; final String value = "Seven"; intToObjectMap.put(key, value); final String newValue = "New Seven"; final String oldValue = intToObjectMap.put(key, newValue); assertThat(intToObjectMap.get(key), is(newValue)); assertThat(oldValue, is(value)); assertThat(intToObjectMap.size(), is(1)); }
@Test public void shouldGrowWhenThresholdExceeded() { final float loadFactor = 0.5f; final int initialCapacity = 32; final Int2ObjectHashMap<String> map = newMap(loadFactor, initialCapacity); for (int i = 0; i < 16; i++) { map.put(i, Integer.toString(i)); } assertThat(map.resizeThreshold(), is(16)); assertThat(map.capacity(), is(initialCapacity)); assertThat(map.size(), is(16)); map.put(16, "16"); assertThat(map.resizeThreshold(), is(initialCapacity)); assertThat(map.capacity(), is(64)); assertThat(map.size(), is(17)); assertThat(map.get(16), equalTo("16")); assertThat((double)loadFactor, closeTo(map.loadFactor(), 0.0f)); }
@DoNotSub public int size() { return Int2ObjectHashMap.this.size(); }
/** * Compact the {@link Map} backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { @DoNotSub final int idealCapacity = (int)Math.round(size() * (1.0d / loadFactor)); rehash(findNextPositivePowerOfTwo(Math.max(MIN_CAPACITY, idealCapacity))); }
/** * Compact the {@link Map} backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { @DoNotSub final int idealCapacity = (int)Math.round(size() * (1.0d / loadFactor)); rehash(BitUtil.findNextPositivePowerOfTwo(idealCapacity)); }
public int read(final HistogramLogHandler handler) throws IOException { remapIfExpanded(); final int timerCount = idToName.size(); int samplesRead = 0; while (true) { if (buffer.remaining() < SIZE_OF_LONG) { return samplesRead; } buffer.mark(); final long timeStamp = buffer.getLong(); if (timeStamp == 0) { buffer.reset(); return samplesRead; } for (int i = 0; i < timerCount; i++) { final int id = buffer.getInt(); final String name = idToName.get(id); final Histogram histogram = Histogram.decodeFromByteBuffer(buffer, 0); handler.onHistogram(timeStamp, name, histogram); } samplesRead++; } }