AbstractMap Extended by ArrayMap

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - AbstractMap Extended by ArrayMap

This is a Java example program.

[edit] Java Source Code

  • Package: example.hashtablemap
  • File: ArrayMap.java
  1. package example.hashtablemap;
  2.  
  3. import java.io.Serializable;
  4. import java.util.AbstractMap;
  5. import java.util.AbstractSet;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Set;
  10.  
  11. public class ArrayMap extends AbstractMap implements Cloneable, Serializable {
  12.  
  13.     static class Entry implements Map.Entry {
  14. 	protected Object key, value;
  15.  
  16. 	public Entry(Object key, Object value) {
  17. 	    this.key = key;
  18. 	    this.value = value;
  19. 	}
  20.  
  21. 	public Object getKey() {
  22. 	    return key;
  23. 	}
  24.  
  25. 	public Object getValue() {
  26. 	    return value;
  27. 	}
  28.  
  29. 	public Object setValue(Object newValue) {
  30. 	    Object oldValue = value;
  31. 	    value = newValue;
  32. 	    return oldValue;
  33. 	}
  34.  
  35. 	public boolean equals(Object o) {
  36. 	    if (!(o instanceof Map.Entry)) {
  37. 		return false;
  38. 	    }
  39. 	    Map.Entry e = (Map.Entry) o;
  40. 	    return (key == null ? e.getKey() == null : key.equals(e.getKey()))
  41. 		    && (value == null ? e.getValue() == null : value.equals(e.getValue()));
  42. 	}
  43.  
  44. 	public int hashCode() {
  45. 	    int keyHash = (key == null ? 0 : key.hashCode());
  46. 	    int valueHash = (value == null ? 0 : value.hashCode());
  47. 	    return keyHash ^ valueHash;
  48. 	}
  49.  
  50. 	public String toString() {
  51. 	    return key + "=" + value;
  52. 	}
  53.     }
  54.  
  55.     private Set entries = null;
  56.     private ArrayList list;
  57.  
  58.     public ArrayMap() {
  59. 	list = new ArrayList();
  60.     }
  61.  
  62.     public ArrayMap(Map map) {
  63. 	list = new ArrayList();
  64. 	putAll(map);
  65.     }
  66.  
  67.     public ArrayMap(int initialCapacity) {
  68. 	list = new ArrayList(initialCapacity);
  69.     }
  70.  
  71.     public Set entrySet() {
  72. 	if (entries == null) {
  73. 	    entries = new AbstractSet() {
  74. 		public void clear() {
  75. 		    list.clear();
  76. 		}
  77.  
  78. 		public Iterator iterator() {
  79. 		    return list.iterator();
  80. 		}
  81.  
  82. 		public int size() {
  83. 		    return list.size();
  84. 		}
  85. 	    };
  86. 	}
  87. 	return entries;
  88.     }
  89.  
  90.     public Object put(Object key, Object value) {
  91. 	int size = list.size();
  92. 	Entry entry = null;
  93. 	int i;
  94. 	if (key == null) {
  95. 	    for (i = 0; i < size; i++) {
  96. 		entry = (Entry) (list.get(i));
  97. 		if (entry.getKey() == null) {
  98. 		    break;
  99. 		}
  100. 	    }
  101. 	} else {
  102. 	    for (i = 0; i < size; i++) {
  103. 		entry = (Entry) (list.get(i));
  104. 		if (key.equals(entry.getKey())) {
  105. 		    break;
  106. 		}
  107. 	    }
  108. 	}
  109. 	Object oldValue = null;
  110. 	if (i < size) {
  111. 	    oldValue = entry.getValue();
  112. 	    entry.setValue(value);
  113. 	} else {
  114. 	    list.add(new Entry(key, value));
  115. 	}
  116. 	return oldValue;
  117.     }
  118.  
  119.     public Object clone() {
  120. 	return new ArrayMap(this);
  121.     }
  122.  
  123. }

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools