public void testPutAll_Map1() { MultiMap original = new MultiHashMap(); original.put("key", "object1"); original.put("key", "object2"); MultiHashMap test = new MultiHashMap(); test.put("keyA", "objectA"); test.put("key", "object0"); test.putAll(original); assertEquals(2, test.size()); assertEquals(4, test.totalSize()); assertEquals(1, test.getCollection("keyA").size()); assertEquals(3, test.getCollection("key").size()); assertEquals(true, test.containsValue("objectA")); assertEquals(true, test.containsValue("object0")); assertEquals(true, test.containsValue("object1")); assertEquals(true, test.containsValue("object2")); }
/** * Clones the map creating an independent copy. * <p> * The clone will shallow clone the collections as well as the map. * * @return the cloned map */ public Object clone() { MultiHashMap cloned = (MultiHashMap) super.clone(); // clone each Collection container for (Iterator it = cloned.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Collection coll = (Collection) entry.getValue(); Collection newColl = createCollection(coll); entry.setValue(newColl); } return cloned; }
public void testGetCollection() { MultiHashMap map = new MultiHashMap(); map.put("A", "AA"); assertSame(map.get("A"), map.getCollection("A")); }
/** * Adds the value to the collection associated with the specified key. * <p> * Unlike a normal <code>Map</code> the previous value is not replaced. * Instead the new value is added to the collection stored against the key. * * @param key the key to store against * @param value the value to add to the collection at the key * @return the value added if the map changed and null if the map did not change */ public Object put(Object key, Object value) { // NOTE:: put is called during deserialization in JDK < 1.4 !!!!!! // so we must have a readObject() Collection coll = getCollection(key); if (coll == null) { coll = createCollection(null); super.put(key, coll); } boolean results = coll.add(value); return (results ? value : null); }
/** * Removes a specific value from map. * <p> * The item is removed from the collection mapped to the specified key. * Other values attached to that key are unaffected. * <p> * If the last value for a key is removed, <code>null</code> will be returned * from a subsequant <code>get(key)</code>. * * @param key the key to remove from * @param item the value to remove * @return the value removed (which was passed in), null if nothing removed */ public Object remove(Object key, Object item) { Collection valuesForKey = getCollection(key); if (valuesForKey == null) { return null; } boolean removed = valuesForKey.remove(item); if (removed == false) { return null; } // remove the list if it is now empty // (saves space, and allows equals to work) if (valuesForKey.isEmpty()){ remove(key); } return item; }
public void testConstructorCopy1() { MultiHashMap map = new MultiHashMap(); map.put("A", "1"); map.put("A", "2"); Collection coll = (Collection) map.get("A"); assertEquals(1, map.size()); assertEquals(2, coll.size()); MultiHashMap newMap = new MultiHashMap(map); Collection newColl = (Collection) newMap.get("A"); assertNotSame(map, newMap); assertNotSame(coll, newColl); assertEquals(1, map.size()); assertEquals(2, coll.size()); assertEquals(1, newMap.size()); assertEquals(2, newColl.size()); map.put("A", "3"); assertEquals(1, map.size()); assertEquals(3, coll.size()); assertEquals(1, newMap.size()); assertEquals(2, newColl.size()); }
MultiHashMap mp = new MultiHashMap(); mp.put("a", "1"); mp.put("b", "4"); mp.put("c", "2"); mp.put("a", "6"); List list = null; Set set = mp.entrySet(); Iterator i = set.iterator(); while (i.hasNext()) { Map.Entry<String, List<String>> me = (Map.Entry) i.next(); for(int j = 0 ; j< me.getValue().size(); j++ ){ System.out.println(me.getKey() +" : " +me.getValue().get(j)); } } }
/** * Override superclass to ensure that MultiMap instances are * correctly handled. * <p> * NOTE: Prior to version 3.2, putAll(map) did not work properly * when passed a MultiMap. * * @param map the map to copy (either a normal or multi map) */ public void putAll(Map map) { if (map instanceof MultiMap) { for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Collection coll = (Collection) entry.getValue(); putAll(entry.getKey(), coll); } } else { for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); put(entry.getKey(), entry.getValue()); } } }
public void testMapEquals() { MultiHashMap one = new MultiHashMap(); Integer value = new Integer(1); one.put("One", value); one.remove("One", value); MultiHashMap two = new MultiHashMap(); assertEquals(two, one); }
public void testTotalSize() { MultiHashMap map = new MultiHashMap(); assertEquals(0, map.totalSize()); map.put("A", "AA"); assertEquals(1, map.totalSize()); map.put("B", "BA"); assertEquals(2, map.totalSize()); map.put("B", "BB"); assertEquals(3, map.totalSize()); map.put("B", "BC"); assertEquals(4, map.totalSize()); map.remove("A"); assertEquals(3, map.totalSize()); map.remove("B", "BC"); assertEquals(2, map.totalSize()); }
public void testSize_Key() { MultiHashMap map = new MultiHashMap(); assertEquals(0, map.size("A")); assertEquals(0, map.size("B")); map.put("A", "AA"); assertEquals(1, map.size("A")); assertEquals(0, map.size("B")); map.put("B", "BA"); assertEquals(1, map.size("A")); assertEquals(1, map.size("B")); map.put("B", "BB"); assertEquals(1, map.size("A")); assertEquals(2, map.size("B")); map.put("B", "BC"); assertEquals(1, map.size("A")); assertEquals(3, map.size("B")); map.remove("A"); assertEquals(0, map.size("A")); assertEquals(3, map.size("B")); map.remove("B", "BC"); assertEquals(0, map.size("A")); assertEquals(2, map.size("B")); }
public void testPutNGet() { MultiHashMap map = new MultiHashMap(); loadMap(map); checkMap(map); assertTrue(map.get(new Integer(99)) == null); map.clear(); assertTrue(map.size() == 0); }
public void testContainsValue_Key() { MultiHashMap map = new MultiHashMap(); assertEquals(false, map.containsValue("A", "AA")); assertEquals(false, map.containsValue("B", "BB")); map.put("A", "AA"); assertEquals(true, map.containsValue("A", "AA")); assertEquals(false, map.containsValue("A", "AB")); }
public void testIterator_Key() { MultiHashMap map = new MultiHashMap(); assertEquals(false, map.iterator("A").hasNext()); map.put("A", "AA"); Iterator it = map.iterator("A"); assertEquals(true, it.hasNext()); it.next(); assertEquals(false, it.hasNext()); }
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { MultiHashMap map = new MultiHashMap(); while (reader.hasMoreChildren()) { reader.moveDown(); Object o = 0; if ("dataStore".equals(reader.getNodeName())) { o = context.convertAnother(map, DataStoreInfoImpl.class); } else { o = context.convertAnother(map, CoverageStoreInfoImpl.class); } map.put(o.getClass(), o); reader.moveUp(); } return map; } }
/** * Gets the collection mapped to the specified key. * This method is a convenience method to typecast the result of <code>get(key)</code>. * * @param key the key to retrieve * @return the collection mapped to the key, null if no mapping * @since Commons Collections 3.1 */ public Collection getCollection(Object key) { return (Collection) get(key); }
/** * Read the object during deserialization. */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { // This method is needed because the 1.2/1.3 Java deserialisation called // put and thus messed up that method // default read object s.defaultReadObject(); // problem only with jvm <1.4 String version = "1.2"; try { version = System.getProperty("java.version"); } catch (SecurityException ex) { // ignore and treat as 1.2/1.3 } if (version.startsWith("1.2") || version.startsWith("1.3")) { for (Iterator iterator = entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); // put has created a extra collection level, remove it super.put(entry.getKey(), ((Collection) entry.getValue()).iterator().next()); } } }
public void testValues() { MultiHashMap map = new MultiHashMap(); loadMap(map); Collection vals = map.values(); assertTrue(vals.size() == getFullSize()); map.clear(); }
public void testConstructorCopy2() { Map map = new HashMap(); map.put("A", "1"); map.put("B", "2"); assertEquals(2, map.size()); MultiHashMap newMap = new MultiHashMap(map); Collection newColl = (Collection) newMap.get("A"); assertNotSame(map, newMap); assertEquals(2, map.size()); assertEquals(2, newMap.size()); assertEquals(1, newColl.size()); map.put("A", "3"); assertEquals(2, map.size()); assertEquals(2, newMap.size()); assertEquals(1, newColl.size()); map.put("C", "4"); assertEquals(3, map.size()); assertEquals(2, newMap.size()); assertEquals(1, newColl.size()); }