public Iterator<E>iterator( ) { return _map.keySet().iterator(); } }
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(); } }
private void checkSizes(int expectedSize) { assertEquals("size()", _nbhm.size(), expectedSize); Collection<String> vals = _nbhm.values(); checkSizes("values()", vals.size(), vals.iterator(), expectedSize); Set<String> keys = _nbhm.keySet(); checkSizes("keySet()", keys.size(), keys.iterator(), expectedSize); Set<Map.Entry<String, String>> ents = _nbhm.entrySet(); checkSizes("entrySet()", ents.size(), ents.iterator(), expectedSize); }
@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(); }
public Iterator<E>iterator( ) { return _map.keySet().iterator(); } }
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(); } }