Map List : Map « Collections Data Structure « Java






Map List

     
/*
 * Copyright (c) 2006-2008 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

//package com.dmdirc.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Wraps a Map<S, List<T>> with various convenience methods for
 * accessing the data. Implements a Map-like interface for easier transition.
 * 
 * @param <S> the type of keys maintained by this map
 * @param <T> the type of mapped values
 * @author chris
 */
public class MapList<S,T> {
    
    /** Our internal map. */
    protected final Map<S, List<T>> map;

    /**
     * Creates a new, empty MapList.
     */
    public MapList() {
        map = new HashMap<S, List<T>>();
    }

    /**
     * Creates a new MapList with the values from the specified list.
     * 
     * @param list The MapList whose values should be used
     */
    public MapList(final MapList<S,T> list) {
        map = list.getMap();
    }

    /**
     * Determines if this MapList is empty. An empty MapList is one that either
     * contains no keys, or contains only keys which have no associated values.
     * 
     * @return True if this MapList is empty, false otherwise
     */
    public boolean isEmpty() {
        for (List<T> list : map.values()) {
            if (!list.isEmpty()) {
                return false;
            }
        }
        
        return true;
    }

    /**
     * Determines if this MapList contains the specified key.
     * 
     * @param key The key to look for
     * @return True if this MapList contains the specified key, false otherwise
     */
    public boolean containsKey(final S key) {
        return map.containsKey(key);
    }

    /**
     * Determines if this MapList contains the specified value as a child of
     * the specified key.
     * 
     * @param key The key to search under
     * @param value The value to look for
     * @return True if this MapList contains the specified key/value pair, 
     * false otherwise
     */
    public boolean containsValue(final S key, final T value) {
        return map.containsKey(key) && map.get(key).contains(value);
    }

    /**
     * Retrieves the list of values associated with the specified key.
     * 
     * @param key The key whose values are being retrieved
     * @return The values belonging to the specified key
     */
    public List<T> get(final S key) {
        return map.get(key);
    }
    
    /**
     * Retrieves the value at the specified offset of the specified key.
     * 
     * @param key The key whose values are being retrieved
     * @param index The index of the value to retrieve
     * @return The specified value of the key
     */    
    public T get(final S key, final int index) {
        return map.get(key).get(index);
    }    
    
    /**
     * Retrieves the list of values associated with the specified key, creating
     * the key if neccessary.
     * 
     * @param key The key to retrieve
     * @return A list of the specified key's values
     */
    public List<T> safeGet(final S key) {
        if (!map.containsKey(key)) {
            map.put(key, new ArrayList<T>());
        }
        
        return map.get(key);
    }
    
    /**
     * Adds the specified key to the MapList.
     * 
     * @param key The key to be added
     */
    public void add(final S key) {
        safeGet(key);
    }    

    /**
     * Adds the specified value as a child of the specified key. If the key
     * didn't previous exist, it is created.
     * 
     * @param key The key to which the value is being added
     * @param value The value to be added
     */
    public void add(final S key, final T value) {
        safeGet(key).add(value);
    }

    /**
     * Adds the specified set of values to the specified key. If the key
     * didn't previous exist, it is created.
     * 
     * @param key The key to which the value is being added
     * @param values The values to be added
     */    
    public void add(final S key, final Collection<T> values) {
        safeGet(key).addAll(values);
    }    

    /**
     * Removes the specified key and all of its values.
     * 
     * @param key The key to remove
     */    
    public void remove(final S key) {
        map.remove(key);
    }
    
    /**
     * Removes the specified value from all keys.
     * 
     * @param value The value to remove
     */
    public void removeFromAll(final T value) {
        for (List<T> list : map.values()) {
            list.remove(value);
        }
    }

    /**
     * Removes the specified value from the specified key.
     * 
     * @param key The key whose value is being removed
     * @param value The value to be removed
     */
    public void remove(final S key, final T value) {
        if (map.containsKey(key)) {
            map.get(key).remove(value);
        }
    }    

    /**
     * Entirely clears this MapList.
     */
    public void clear() {
        map.clear();
    }
    
    /**
     * Clears all values of the specified key.
     * 
     * @param key The key to be cleared
     */
    public void clear(final S key) {
        safeGet(key).clear();
    }    

    /**
     * Returns the set of all keys belonging to this MapList.
     * 
     * @return This MapList's keyset
     */
    public Set<S> keySet() {
        return map.keySet();
    }

    /**
     * Returns a collection of all values belonging to the specified key.
     * 
     * @param key The key whose values are being sought
     * @return A collection of values belonging to the key
     */
    public Collection<T> values(final S key) {
        return map.get(key);
    }
    
    /**
     * Retrieves the entry set for this MapList.
     * 
     * @return This MapList's entry set
     */
    public Set<Map.Entry<S, List<T>>> entrySet() {
        return map.entrySet();
    }
    
    /**
     * Retrieves the map behind this maplist.
     * 
     * @return This MapList's map.
     */
    public Map<S, List<T>> getMap() {
        return new HashMap<S, List<T>>(map);
    }

}
/*

package com.dmdirc.util;

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;

public class MapListTest extends junit.framework.TestCase {

    @Test
    public void testIsEmpty() {
        final MapList<String, String> test = new MapList<String, String>();
        assertTrue(test.isEmpty());

        test.add("a", "b");
        assertFalse(test.isEmpty());
        test.removeFromAll("b");
        assertTrue(test.isEmpty());
    }
    
    @Test
    public void testAddCollection() {
        final MapList<String, String> test = new MapList<String, String>();
        final List<String> testList = new ArrayList<String>();
        testList.add("d");
        testList.add("e");
        test.add("key", testList);
        
        assertTrue(test.containsKey("key"));
        assertTrue(test.containsValue("key", "d"));
        assertTrue(test.containsValue("key", "e"));
    }
    
    @Test
    public void testClear() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        test.add("d", "e");
        test.clear();
        assertTrue(test.isEmpty());
    }
    
    @Test
    public void testClearKey() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        test.add("d", "e");
        test.clear("a");
        assertTrue(test.values("a").isEmpty());
        assertFalse(test.isEmpty());
    }
    
    @Test
    public void testRemove() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        test.add("d", "e");
        test.remove("z", "b");
        
        assertEquals(2, test.keySet().size());
        assertEquals(1, test.values("a").size());
        assertEquals(1, test.values("d").size());
        
        test.remove("a", "b");
        assertEquals(2, test.keySet().size());
        assertEquals(0, test.values("a").size());
        assertEquals(1, test.values("d").size());        
    }    
    
    @Test
    public void testKeySet() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        test.add("d", "e");
        assertEquals(2, test.keySet().size());
        assertTrue(test.keySet().contains("a"));
        assertTrue(test.keySet().contains("d"));
    }    

    @Test
    public void testContainsKey() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        assertTrue(test.containsKey("a"));
    }

    @Test
    public void testContainsValue() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        assertTrue(test.containsValue("a", "b"));
    }

    @Test
    public void testGet() {
        final MapList<String, String> test = new MapList<String, String>();
        test.add("a", "b");
        assertTrue(test.get("a").size() == 1);
        assertTrue(test.get("a").get(0).equals("b"));
        assertTrue(test.get("a", 0).equals("b"));
    }

}

*/

   
    
    
    
    
  








Related examples in the same category

1.Creating and storing arrays in a map
2.Sort based on the values
3.Get a key from value with an HashMap
4.Retrieve environment variables (JDK1.5)
5.Creating a Type-Specific Map: creates a map whose keys are Integer objects and values are String objects.
6.A map declared to hold objects of a type T can also hold objects that extend from T
7.A value retrieved from a type-specific collection does not need to be casted
8.Map techniques.
9.Create an array containing the keys in a map
10.Create an array containing the values in a map
11.Creating a Hash Table
12.Creating a Map That Retains Order-of-Insertion
13.Automatically Removing an Unreferenced Element from a Hash Table
14.Creating a Type-Specific Map [5.0]
15.Use Iterator to loop through the HashMap class
16.Create type specific collections
17.Convert Properties into Map
18.A java.util.Map implementation using reference values
19.Utility method that return a String representation of a map. The elements will be represented as "key = value"
20.Utility method that return a String representation of a map. The elements will be represented as "key = value" (tab)
21.This program demonstrates the use of a map with key type String and value type Employee
22.Format a Map
23.A Map implementation that dumps its content when memory runs low.
24.A Map that stores the values in files within a directory.
25.Multi Value Map Array List
26.Multi Value Map Linked HashSet
27.An object that maps keys to values, and values back to keys.
28.LRU Map
29.A map acts like array.
30.Order Retaining Map
31.BinaryMap class implements a map from objects to integer objects where the only value is the integer with value 1.
32.A space-optimized map for associating char keys with values.
33.A Map implementation that grows to a fixed size and then retains only a fixed number of the highest (largest) keys.
34.Class which creates mapping between keys and a list of values.
35.A map of values by class.
36.History Map
37.Sorts map by values in ascending order.
38.Map from a given key to a list of values
39.Map from a given key to a set of values
40.Class which keeps a set of values and assigns each value a unique positive index.
41.Array Map
42.Array map
43.An ArrayMap is a very inefficient map type that is more robust in dealing with changes to its keys than other maps.
44.This Map stores it's keys as strings in upper case, null and duplicate keys are not allowed
45.Map to string
46.A simple class that stores key Strings as char[]'s in a hash table.