Custom ArrayMap implementation (extends AbstractMap) : Array Collections « 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 » Array CollectionsScreenshots 
Custom ArrayMap implementation (extends AbstractMap)
Custom ArrayMap implementation (extends AbstractMap)
    
import java.io.Serializable;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ArrayMap extends AbstractMap implements Cloneable, Serializable {

  static class Entry implements Map.Entry {
    protected Object key, value;

    public 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 newValue) {
      Object oldValue = value;
      value = newValue;
      return oldValue;
    }

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

    public int hashCode() {
      int keyHash = (key == null : key.hashCode());
      int valueHash = (value == null : value.hashCode());
      return keyHash ^ valueHash;
    }

    public String toString() {
      return key + "=" + value;
    }
  }

  private Set entries = null;

  private ArrayList list;

  public ArrayMap() {
    list = new ArrayList();
  }

  public ArrayMap(Map map) {
    list = new ArrayList();
    putAll(map);
  }

  public ArrayMap(int initialCapacity) {
    list = new ArrayList(initialCapacity);
  }

  public Set entrySet() {
    if (entries == null) {
      entries = new AbstractSet() {
        public void clear() {
          list.clear();
        }

        public Iterator iterator() {
          return list.iterator();
        }

        public int size() {
          return list.size();
        }
      };
    }
    return entries;
  }

  public Object put(Object key, Object value) {
    int size = list.size();
    Entry entry = null;
    int i;
    if (key == null) {
      for (i = 0; i < size; i++) {
        entry = (Entry) (list.get(i));
        if (entry.getKey() == null) {
          break;
        }
      }
    else {
      for (i = 0; i < size; i++) {
        entry = (Entry) (list.get(i));
        if (key.equals(entry.getKey())) {
          break;
        }
      }
    }
    Object oldValue = null;
    if (i < size) {
      oldValue = entry.getValue();
      entry.setValue(value);
    else {
      list.add(new Entry(key, value));
    }
    return oldValue;
  }

  public Object clone() {
    return new ArrayMap(this);
  }

  public static void main(String args[]) {
    Map map = new ArrayMap(13);
    map.put("1""One");
    map.put("2""Two");
    map.put("3""Three");
    map.put("4""Four");
    map.put("5""Five");
    map.put("6""Six");
    map.put("7""Seven");
    map.put("8""Eight");
    map.put("9""Nine");
    map.put("10""Ten");
    map.put("11""Eleven");
    map.put("12""Twelve");
    map.put("13""Thirteen");
    System.out.println(map);
    System.out.println(map.keySet());
    System.out.println(map.values());
  }
}
           
         
    
    
    
  
Related examples in the same category
1. Array Iterator
2. Array MapArray Map
3. Array SetArray Set
4. Array Int Set
5. Remove duplicate element from array
6. Convert an Array to a List
7. Converting an Array to a Collection
8. Converting a Collection of user objects to an Array
9. Create an array containing the elements in a set
10. Convert an array to a Map
11. Converting a Collection of String to an ArrayConverting a Collection of String to an Array
12. Treating an Array as an Enumeration
13. ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
14. Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
15. Converts array into a java.util.Map.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.