/** Tests if the key in the table using the <tt>equals</tt> method. * @return <tt>true</tt> if the key is in the table using the <tt>equals</tt> method * @throws NullPointerException if the specified key is null */ @Override public boolean containsKey( Object key ) { return get(key) != null; }
private void writeObject(java.io.ObjectOutputStream s) throws IOException { s.defaultWriteObject(); // Nothing to write for( Object K : keySet() ) { final Object V = get(K); // Do an official 'get' s.writeObject(K); // Write the <TypeK,TypeV> pair s.writeObject(V); } s.writeObject(null); // Sentinel to indicate end-of-data s.writeObject(null); }
public int run_normal(NonBlockingHashMap<String, String> hm) { SimpleRandom R = new SimpleRandom(); int get_ops = 0; int put_ops = 0; int del_ops = 0; while (!_stop) { int x = R.nextInt() & ((1 << 20) - 1); String key = KEYS[R.nextInt() & (KEYS.length - 1)]; if (x < _gr) { get_ops++; String val = hm.get(key); if (val != null && !val.equals(key)) throw new IllegalArgumentException("Mismatched key=" + key + " and val=" + val); } else if (x < _pr) { put_ops++; hm.putIfAbsent(key, key); } else { del_ops++; hm.remove(key); } } // We stopped; report results into shared result structure return get_ops + put_ops + del_ops; }
/** * 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 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)); }
sum += x; assertTrue(x >= 0 && x <= (CNT - 1)); String v = _nbhm.get(s); assertThat("", v.charAt(0), is('v')); assertThat("", s.substring(1), is(v.substring(1)));
assertThat(_nbhm.put("k0", "v0"), nullValue()); assertEquals(_nbhm.replace("k0", "v0a"), "v0"); assertEquals(_nbhm.get("k0"), "v0a"); assertThat(_nbhm.remove("k0"), is("v0a")); assertFalse(_nbhm.containsKey("k0")); assertThat(_nbhm.put("k1", "v1"), nullValue()); assertEquals(_nbhm.replace("k1", "v1a"), "v1"); assertEquals(_nbhm.get("k1"), "v1a"); assertThat(_nbhm.remove("k1"), is("v1a")); assertFalse(_nbhm.containsKey("k1")); dumb.get(happy2); map.get(33); // this returns null, but tested a crash edge case for expansion
/** Tests if the key in the table using the <tt>equals</tt> method. * @return <tt>true</tt> if the key is in the table using the <tt>equals</tt> method * @throws NullPointerException if the specified key is null */ @Override public boolean containsKey( Object key ) { return get(key) != null; }
private void writeObject(java.io.ObjectOutputStream s) throws IOException { s.defaultWriteObject(); // Nothing to write for( Object K : keySet() ) { final Object V = get(K); // Do an official 'get' s.writeObject(K); // Write the <TypeK,TypeV> pair s.writeObject(V); } s.writeObject(null); // Sentinel to indicate end-of-data s.writeObject(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(); } }