SmallArrayBasedLongToDoubleMap { public boolean put(long key, double value, long metadata) { boolean isUniqueKey = true; if (size < ADD_KEYS_TO_SET_THRESHOLD) { for (int i = 0; i < size; i++) { if (key == keys[i]) { isUniqueKey = false; if (metadata == metadataArray[i]) { return false; } } } } else { if (keySet == null) { keySet = new LongOpenHashSet(keys, 0.75f ); keyMetadataPairSet = new ObjectOpenHashSet<>(); for (int i = 0; i < size; i++) { keyMetadataPairSet.add(new Pair(keys[i], metadataArray[i])); } } Pair pair = new Pair(key, metadata); if (keyMetadataPairSet.contains(pair)) { return false; } else { isUniqueKey = keySet.add(key); keyMetadataPairSet.add(pair); } } if (size == capacity) { capacity = 2 * capacity; copy(capacity, size); } if (isUniqueKey) uniqueKeysSize++; keys[size] = key; values[size] = value; metadataArray[size] = metadata; size++; return true; } SmallArrayBasedLongToDoubleMap(); long[] keys(); double[] values(); long[] metadata(); int size(); int uniqueKeysSize(); boolean put(long key, double value, long metadata); void sort(); boolean trim(int inputCapacity); boolean contains(long key);  }
@Test public void testRepeatedKeyMetadataPairs() { SmallArrayBasedLongToDoubleMap map = new SmallArrayBasedLongToDoubleMap(); for (int i = 1; i <= 100; i++) { map.put(i, i * 0.01, 1000 + i); } for (int i = 1; i <= 100; i++) { assertEquals(false, map.put(i, i * 0.01, 1000 + i)); } for (int i = 100; i >= 1; i--) { assertEquals(false, map.put(i, i * 0.01, 1000 + i)); } for (int i = 1; i <= 100; i++) { assertEquals(true, map.put(i, i * 0.01, 2000 + i)); } }