MultiMap extends AbstractMap : HashTable Map « Collections Data Structure « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » HashTable MapScreenshots 
MultiMap extends AbstractMap
     
import java.util.*;

public class MultiMap extends AbstractMap {
    
  private Map map;

  public MultiMap() {
    this(null);
  }

  public MultiMap(Map copy) {
    map = new HashMap();
    if (copy != null) {
      Iterator iter = copy.entrySet().iterator();
      while(iter.hasNext()) {
        Map.Entry entry = (Map.Entry)iter.next();
        add(entry.getKey(), entry.getValue());
      }
    }
  }

  public boolean containsKey(Object key) {
    Collection values = (Collection)map.get(key);
    return ((values != null&& (values.size() != 0));
  }
    
  public boolean containsValue(Object value) {
    Iterator iter = map.entrySet().iterator();
    boolean found = false;
    while (iter.hasNext()) {
      Map.Entry entry = (Map.Entry)iter.next();
      Collection values = (Collection)entry.getValue();
      if (values.contains(value)) {
        found = true;
        break;
      }
    }
    return found;
  }


  public Object get(Object key) {
    return map.get(key);
  }

  public Object put(Object key, Object value) {
    if (!(value instanceof Collection)) {
      throw new IllegalArgumentException(value.getClass().toString());
    }
    Object original = get(key);
    map.put(key, value);
    return original;
  }

  public boolean add(Object key, Object value) {
    return getValues(key).add(value);
  }
    
  public boolean addAll(Object key, Collection values) {
    return getValues(key).addAll(values);
  }

  private Collection getValues(Object key) {
    Collection col = (Collection)map.get(key);
    if (col == null) {
      col = new HashSet();
      map.put(key, col);
    }
    return col;
  }

  public Object remove(Object key) {
    Object original = get(key);
    map.remove(key);
    return original;
  }

  public boolean remove(Object key, Object value) {
    Collection values = (Collection)map.get(key);
    if (values == null) {
      return false;
    else {
      return values.remove(value);
    }
  }

  public void clear() {
    map.clear();
  }

  public String toString() {
    StringBuffer buff = new StringBuffer();
    buff.append("{");
    Iterator keys = map.keySet().iterator();
    boolean first = true;
    while (keys.hasNext()) {
      if (first) {
        first = false;
      else {
        buff.append(", ");
      }
      Object key = keys.next();
      Collection values = getValues(key);
      buff.append("[" + key + ": " + values + "]");
    }
    buff.append("}");
    return buff.toString();
  }

  public Set entrySet() {
    int size = 0;
    Iterator iterKeys = map.entrySet().iterator();
    while (iterKeys.hasNext()) {
      Map.Entry entry = (Map.Entry)iterKeys.next();
      Collection values = (Collection)entry.getValue();
      Iterator iterValues = values.iterator();
      while (iterValues.hasNext()) { 
        size++;
        iterValues.next();
      }
    }

    final int finalSize = size;

    final Iterator entries = map.entrySet().iterator();

    return new AbstractSet() {
      int pos = 0;
      Map.Entry entry;
      Iterator values;

      public Iterator iterator() { 
        return new Iterator() {
          public void remove() {
            throw new UnsupportedOperationException();
          }
          public boolean hasNext() {
            return pos != finalSize;
          }
          public Object next() {
            while(true) {
              if (entry == null) {
                entry = (Map.Entry)entries.next();
                values = ((Collection)entry.getValue()).iterator();
              }
              Object key = entry.getKey();
              if (values.hasNext()) {
                Object value = values.next();
                pos++;
                return new Entry(key, value);
              else {
                entry = null;
              }
            }
          }
        };
      }
      public int size() {
        return finalSize;
      }
    };
  }

  private static class Entry implements Map.Entry {
    Object key;
    Object value;

    Entry(Object key, Object value) {
      this.key = key;
      this.value = value;
    }

    public Object getKey() {
      return key;
    }

    public Object getValue() {
      return value;
    }

    public Object setValue(Object value) {
      Object oldValue = this.value;
      this.value = value;
      return oldValue;
    }

    public boolean equals(Object o) {
      if (!(instanceof Map.Entry)) {
        return false;
      else {
        Map.Entry e = (Map.Entry)o;
        return (key==null ? e.getKey()==null : key.equals(e.getKey())) &&
          (value==null ? e.getValue()==null : value.equals(e.getValue()));
      }
    }

    public int hashCode() {
      return ((value==null: value.hashCode());
    }

    public String toString() {
      return key+"="+value;
    }
  }
  public static void main (String args[]) {
    Map map = new HashMap();
    map.put("one""two");
    map.put("three""four");
    map.put("five""six");
    MultiMap multi = new MultiMap(map);
    System.out.println(multi);
    multi.add("five""seven");
    multi.add("five""eight");
    multi.add("five""nine");
    multi.add("five""ten");
    multi.add("three""seven");
    System.out.println(multi);
    multi.remove("three");
    System.out.println(multi);
  }
}


           
         
    
    
    
    
  
Related examples in the same category
1. Check if a particular key exists in Java Hashtable
2. Check if a particular value exists in Java Hashtable
3. Get Collection of Values from Java Hashtable
4. Get Set view of Keys from Java Hashtable
5. Get Size of Java Hashtable
6. Iterate through keys of Java Hashtable
7. Remove all values from Java Hashtable
8. Scan the content of a hashtable
9. Remove value from Java Hashtable
10. Sort keys in an Hashtable
11. Associates keys with valuesAssociates keys with values
12. Iterate through values of Java Hashtable
13. A simple Map implementationA simple Map implementation
14. Hash table with separate chaining
15. Hash table with linear probingHash table with linear probing
16. Hash table with double hashingHash table with double hashing
17. Working with Key-Value Pairs in a Hashtable
18. Demonstrate the Hashtable class, and an Enumeration
19. Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
20. Soft HashMap
21. Array Map extends AbstractMapArray Map extends AbstractMap
22. Demonstrating the WeakHashMapDemonstrating the WeakHashMap
23. Use treemapUse treemap
24. Sorting Elements in a TreeMapSorting Elements in a TreeMap
25. What you can do with a TreeMapWhat you can do with a TreeMap
26. A Map implemented with ArrayLists
27. Simple demonstration of HashMapSimple demonstration of HashMap
28. HashMap
29. Caching Hashtable
30. Hashtable that supports mostly-concurrent reading, but exclusive writing.
31. Lru Hashtable
32. Bucketized Hashtable
33. Custom hash table based on customized array
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.