@Test public void testPairsMatching() { DoubleArrayList keyList = new DoubleArrayList(); List<TestClass> valueList = new ArrayList<TestClass>(); OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem2); map.put((double) 12, anotherItem3); map.put((double) 13, anotherItem4); map.put((double) 14, anotherItem5); map.removeKey((double) 13); map.pairsMatching( new DoubleObjectProcedure<TestClass>() { @Override public boolean apply(double first, TestClass second) { return (first % 2) == 0; } }, keyList, valueList); keyList.sort(); Collections.sort(valueList); assertEquals(2, keyList.size()); assertEquals(2, valueList.size()); assertEquals(12, keyList.get(0), (double) 0.000001); assertEquals(14, keyList.get(1), (double) 0.000001); assertSame(anotherItem3, valueList.get(0)); assertSame(anotherItem5, valueList.get(1)); }
@Test public void testPairsSortedByKey() { OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 100); map.put((short) 12, (double) 70); map.put((short) 13, (double) 30); map.put((short) 14, (double) 3); ShortArrayList keys = new ShortArrayList(); DoubleArrayList values = new DoubleArrayList(); map.pairsSortedByKey(keys, values); assertEquals(4, keys.size()); assertEquals(4, values.size()); assertEquals((short) 11, keys.get(0)); assertEquals((double) 100, values.get(0), (double) 0.000001); assertEquals((short) 12, keys.get(1)); assertEquals((double) 70, values.get(1), (double) 0.000001); assertEquals((short) 13, keys.get(2)); assertEquals((double) 30, values.get(2), (double) 0.000001); assertEquals((short) 14, keys.get(3)); assertEquals((double) 3, values.get(3), (double) 0.000001); keys.clear(); values.clear(); map.pairsSortedByValue(keys, values); assertEquals((short) 11, keys.get(3)); assertEquals((double) 100, values.get(3), (double) 0.000001); assertEquals((short) 12, keys.get(2)); assertEquals((double) 70, values.get(2), (double) 0.000001); assertEquals((short) 13, keys.get(1)); assertEquals((double) 30, values.get(1), (double) 0.000001); assertEquals((short) 14, keys.get(0)); assertEquals((double) 3, values.get(0), (double) 0.000001); }
@Test public void testPairsMatching() { ShortArrayList keyList = new ShortArrayList(); DoubleArrayList valueList = new DoubleArrayList(); OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 22); map.put((short) 12, (double) 23); map.put((short) 13, (double) 24); map.put((short) 14, (double) 25); map.removeKey((short) 13); map.pairsMatching( new ShortDoubleProcedure() { @Override public boolean apply(short first, double second) { return (first % 2) == 0; } }, keyList, valueList); keyList.sort(); valueList.sort(); assertEquals(2, keyList.size()); assertEquals(2, valueList.size()); assertEquals(12, keyList.get(0)); assertEquals(14, keyList.get(1)); assertEquals(23, valueList.get(0), (double) 0.000001); assertEquals(25, valueList.get(1), (double) 0.000001); }
@Test public void testKeysSortedByValue() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem5); map.put((double) 12, anotherItem4); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem2); map.removeKey((double) 13); DoubleArrayList keys = new DoubleArrayList(); map.keysSortedByValue(keys); double[] keysArray = keys.toArray(new double[keys.size()]); assertArrayEquals(new double[] {14, 12, 11}, keysArray, (double) 0.000001); }
@Test public void testValues() { OpenShortDoubleHashMap map = new OpenShortDoubleHashMap(); map.put((short) 11, (double) 22); map.put((short) 12, (double) 23); map.put((short) 13, (double) 24); map.put((short) 14, (double) 25); map.removeKey((short) 13); DoubleArrayList values = new DoubleArrayList(100); map.values(values); assertEquals(3, values.size()); values.sort(); assertEquals(22, values.get(0), (double) 0.000001); assertEquals(23, values.get(1), (double) 0.000001); assertEquals(25, values.get(2), (double) 0.000001); }
/** * Returns a string representation of the receiver, containing the String representation of each * key-value pair, sorted ascending by key. */ public String toString() { DoubleArrayList theKeys = keys(); // theKeys.sort(); StringBuilder buf = new StringBuilder(); buf.append('['); int maxIndex = theKeys.size() - 1; for (int i = 0; i <= maxIndex; i++) { double key = theKeys.get(i); buf.append(String.valueOf(key)); if (i < maxIndex) { buf.append(", "); } } buf.append(']'); return buf.toString(); }
@Test public void testPairsSortedByKey() { OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem5); map.put((double) 12, anotherItem4); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem2); DoubleArrayList keys = new DoubleArrayList(); List<TestClass> values = new ArrayList<TestClass>(); map.pairsSortedByKey(keys, values); assertEquals(4, keys.size()); assertEquals(4, values.size()); assertEquals((double) 11, keys.get(0), (double) 0.000001); assertSame(anotherItem5, values.get(0)); assertEquals((double) 12, keys.get(1), (double) 0.000001); assertSame(anotherItem4, values.get(1)); assertEquals((double) 13, keys.get(2), (double) 0.000001); assertSame(anotherItem3, values.get(2)); assertEquals((double) 14, keys.get(3), (double) 0.000001); assertSame(anotherItem2, values.get(3)); }
@Test public void testForEachKey() { final DoubleArrayList keys = new DoubleArrayList(); OpenDoubleObjectHashMap<TestClass> map = new OpenDoubleObjectHashMap<TestClass>(); map.put((double) 11, anotherItem); map.put((double) 12, anotherItem2); map.put((double) 13, anotherItem3); map.put((double) 14, anotherItem4); map.removeKey((double) 13); map.forEachKey( new DoubleProcedure() { @Override public boolean apply(double element) { keys.add(element); return true; } }); double[] keysArray = keys.toArray(new double[keys.size()]); Arrays.sort(keysArray); assertArrayEquals(new double[] {11, 12, 14}, keysArray, (double) 0.000001); }