/** * Int primitive specialised containsKey. * * @param key the key to check. * @return true if the map contains key as a key, false otherwise. */ public boolean containsKey(final int key) { return get(key) != missingValue; }
/** * {@inheritDoc} */ public boolean contains(final Object o) { final Entry entry = (Entry)o; final Integer value = get(entry.getKey()); return value != null && value.equals(entry.getValue()); } }
/** * {@inheritDoc} */ public Integer get(final Object key) { return valOrNull(get((int)key)); }
/** * Primitive specialised version of {@link #replace(Object, Object)} * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * {@link #missingValue()} if there was no mapping for the key. */ public int replace(final int key, final int value) { int curValue = get(key); if (curValue != missingValue) { curValue = put(key, value); } return curValue; }
/** * Primitive specialised version of {@link #replace(Object, Object, Object)} * * @param key key with which the specified value is associated * @param oldValue value expected to be associated with the specified key * @param newValue value to be associated with the specified key * @return {@code true} if the value was replaced */ public boolean replace(final int key, final int oldValue, final int newValue) { final int curValue = get(key); if (curValue != oldValue || curValue == missingValue) { return false; } put(key, newValue); return true; }
/** * Primitive specialised version of {@link #computeIfAbsent(Object, Function)} * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == missingValue) { value = mappingFunction.applyAsInt(key); if (value != missingValue) { put(key, value); } } return value; }
@Test public void boxedGetShouldReturnNull() { assertNull(map.get((Integer)1)); }
@Test public void getShouldReturnMissingValueWhenEmpty() { assertEquals(MISSING_VALUE, map.get(1)); }
@Test public void shouldResizeWhenMoreElementsAreAdded() { IntStream .range(0, 100) .forEach((key) -> { final int value = key * 2; assertEquals(MISSING_VALUE, map.put(key, value)); assertEquals(value, map.get(key)); }); }
@Test public void getShouldReturnMissingValueWhenThereIsNoElement() { map.put(1, 1); assertEquals(MISSING_VALUE, map.get(2)); }
@Test public void getShouldReturnPutValues() { map.put(1, 1); assertEquals(1, map.get(1)); }
@Test public void shouldAllowMissingValueAsKey() { map.put(MISSING_VALUE, 1); assertEquals(1, map.get(MISSING_VALUE)); assertTrue(map.containsKey(MISSING_VALUE)); assertEquals(1, map.size()); final int[] tuple = new int[2]; map.intForEach((k, v) -> { tuple[0] = k; tuple[1] = v; }); assertEquals(MISSING_VALUE, tuple[0]); assertEquals(1, tuple[1]); assertEquals(1, map.remove(MISSING_VALUE)); assertEquals(0, map.size()); assertEquals(MISSING_VALUE, map.get(MISSING_VALUE)); }
/** * Int primitive specialised containsKey. * * @param key the key to check. * @return true if the map contains key as a key, false otherwise. */ public boolean containsKey(final int key) { return get(key) != missingValue; }
/** * Int primitive specialised containsKey. * * @param key the key to check. * @return true if the map contains key as a key, false otherwise. */ public boolean containsKey(final int key) { return get(key) != missingValue; }
/** * {@inheritDoc} */ public Integer get(final Object key) { return get((int)key); }
/** * {@inheritDoc} */ public boolean contains(final Object o) { final Entry entry = (Entry)o; final Integer value = get(entry.getKey()); return value != null && value.equals(entry.getValue()); } }
/** * {@inheritDoc} */ public Integer get(final Object key) { return valOrNull(get((int)key)); }