/** Copies all of the mappings from the specified map to this one, replacing * any existing mappings. * @param m mappings to be stored in this map */ @Override public void putAll(Map<? extends TypeK, ? extends TypeV> m) { for (Map.Entry<? extends TypeK, ? extends TypeV> e : m.entrySet()) put(e.getKey(), e.getValue()); }
checkSizes(2); assertTrue(_nbhm.containsKey("k2")); assertThat(_nbhm.put("k1", "v1a"), is("v1")); assertThat(_nbhm.put("k2", "v2a"), is("v2")); checkSizes(2); assertThat(_nbhm.putIfAbsent("k2", "v2b"), is("v2a")); assertTrue(_nbhm.isEmpty()); assertThat(_nbhm.put("k0", "v0"), nullValue()); assertTrue(_nbhm.containsKey("k0")); checkSizes(1); assertThat(_nbhm.put("k0", "v0"), nullValue()); assertEquals(_nbhm.replace("k0", "v0a"), "v0"); assertEquals(_nbhm.get("k0"), "v0a"); assertThat(_nbhm.put("k1", "v1"), nullValue()); assertEquals(_nbhm.replace("k1", "v1a"), "v1"); assertEquals(_nbhm.get("k1"), "v1a"); assertThat(dumb.put(happy1, "and"), nullValue()); if ((i & 1) == 0) for (int i = 1; i < 32; i++) map.put(i, new Object());
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); // Read nothing initialize(MIN_SIZE); for(;;) { final TypeK K = (TypeK) s.readObject(); final TypeV V = (TypeV) s.readObject(); if( K == null ) break; put(K,V); // Insert with an offical put } }
public Object call() throws Exception { _barrier.await(); // barrier, to force racing start for (long j = 0; j < _count; j++) { _map.put( j + _offset, new TestKey(_rand.nextLong(), _rand.nextInt(), (short) _rand.nextInt(Short.MAX_VALUE))); } return null; } }
/** * Creates a shallow copy of this hashtable. All the structure of the * hashtable itself is copied, but the keys and values are not cloned. * This is a relatively expensive operation. * * @return a clone of the hashtable. */ @Override public Object clone() { try { // Must clone, to get the class right; NBHM might have been // extended so it would be wrong to just make a new NBHM. NonBlockingHashMap<TypeK,TypeV> t = (NonBlockingHashMap<TypeK,TypeV>) super.clone(); // But I don't have an atomic clone operation - the underlying _kvs // structure is undergoing rapid change. If I just clone the _kvs // field, the CHM in _kvs[0] won't be in sync. // // Wipe out the cloned array (it was shallow anyways). t.clear(); // Now copy sanely for( TypeK K : keySet() ) { final TypeV V = get(K); // Do an official 'get' t.put(K,V); } return t; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } }
@Test public void removeResultIsBasedOnEquality() { NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>(); Integer initialValue = new Integer(10); map.put(1, initialValue); assertTrue(map.remove(1, initialValue)); map.put(1, initialValue); assertTrue(map.remove(1, new Integer(10))); }
@Test public void replaceResultIsBasedOnEquality() { NonBlockingHashMap<Integer, Integer> map = new NonBlockingHashMap<>(); Integer initialValue = new Integer(10); map.put(1, initialValue); assertTrue(map.replace(1, initialValue, 20)); assertTrue(map.replace(1, new Integer(20), 30)); }
@Test public void testIterationBig2() { final int CNT = 10000; NonBlockingHashMap<Integer, String> nbhm = new NonBlockingHashMap<>(); final String v = "v"; for (int i = 0; i < CNT; i++) { final Integer z = i; String s0 = nbhm.get(z); assertThat(s0, nullValue()); nbhm.put(z, v); String s1 = nbhm.get(z); assertThat(s1, is(v)); } assertThat(nbhm.size(), is(CNT)); }
@Test public void testIteration() { assertTrue(_nbhm.isEmpty()); assertThat(_nbhm.put("k1", "v1"), nullValue()); assertThat(_nbhm.put("k2", "v2"), nullValue()); String str1 = ""; for (Map.Entry<String, String> e : _nbhm.entrySet()) { str1 += e.getKey(); } assertThat("found all entries", str1, anyOf(is("k1k2"), is("k2k1"))); String str2 = ""; for (String key : _nbhm.keySet()) { str2 += key; } assertThat("found all keys", str2, anyOf(is("k1k2"), is("k2k1"))); String str3 = ""; for (String val : _nbhm.values()) { str3 += val; } assertThat("found all vals", str3, anyOf(is("v1v2"), is("v2v1"))); assertThat("toString works", _nbhm.toString(), anyOf(is("{k1=v1, k2=v2}"), is("{k2=v2, k1=v1}"))); _nbhm.clear(); }
for (int i = 0; i < CNT; i++) _nbhm.put("k" + i, "v" + i);
/** Copies all of the mappings from the specified map to this one, replacing * any existing mappings. * @param m mappings to be stored in this map */ @Override public void putAll(Map<? extends TypeK, ? extends TypeV> m) { for (Map.Entry<? extends TypeK, ? extends TypeV> e : m.entrySet()) put(e.getKey(), e.getValue()); }
private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); // Read nothing initialize(MIN_SIZE); for(;;) { final TypeK K = (TypeK) s.readObject(); final TypeV V = (TypeV) s.readObject(); if( K == null ) break; put(K,V); // Insert with an offical put } }
/** * Creates a shallow copy of this hashtable. All the structure of the * hashtable itself is copied, but the keys and values are not cloned. * This is a relatively expensive operation. * * @return a clone of the hashtable. */ @Override public Object clone() { try { // Must clone, to get the class right; NBHM might have been // extended so it would be wrong to just make a new NBHM. NonBlockingHashMap<TypeK,TypeV> t = (NonBlockingHashMap<TypeK,TypeV>) super.clone(); // But I don't have an atomic clone operation - the underlying _kvs // structure is undergoing rapid change. If I just clone the _kvs // field, the CHM in _kvs[0] won't be in sync. // // Wipe out the cloned array (it was shallow anyways). t.clear(); // Now copy sanely for( TypeK K : keySet() ) { final TypeV V = get(K); // Do an official 'get' t.put(K,V); } return t; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } }