/** Returns <tt>true</tt> if this Map maps one or more keys to the specified * value. <em>Note</em>: This method requires a full internal traversal of the * hash table and is much slower than {@link #containsKey}. * @param val value whose presence in this map is to be tested * @return <tt>true</tt> if this map maps one or more keys to the specified value * @throws NullPointerException if the specified value is null */ @Override public boolean containsValue( final Object val ) { if( val == null ) throw new NullPointerException(); for( TypeV V : values() ) if( V == val || V.equals(val) ) return true; return false; }
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 testNonBlockingHashMapIteratorMultithreaded() throws InterruptedException, ExecutionException { TestKeyFeeder feeder = getTestKeyFeeder(); final int itemCount = feeder.size(); // validate results final NonBlockingHashMap<Long, TestKey> items = feeder.getMapMultithreaded(); assertEquals("size()", itemCount, items.size()); assertEquals("values().size()", itemCount, items.values().size()); assertEquals("entrySet().size()", itemCount, items.entrySet().size()); int iteratorCount = 0; for (TestKey m : items.values()) { iteratorCount++; } // sometimes a different result comes back the second time int iteratorCount2 = 0; for (TestKey m2 : items.values()) { iteratorCount2++; } assertEquals("iterator counts differ", iteratorCount, iteratorCount2); assertEquals("values().iterator() count", itemCount, iteratorCount); }
@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(); }
/** Returns <tt>true</tt> if this Map maps one or more keys to the specified * value. <em>Note</em>: This method requires a full internal traversal of the * hash table and is much slower than {@link #containsKey}. * @param val value whose presence in this map is to be tested * @return <tt>true</tt> if this map maps one or more keys to the specified value * @throws NullPointerException if the specified value is null */ @Override public boolean containsValue( final Object val ) { if( val == null ) throw new NullPointerException(); for( TypeV V : values() ) if( V == val || V.equals(val) ) return true; return false; }