/home/caleb/ASDV-Java/Semester 4/Assignments/MapASDV_CalebFontenot/src/mapasdv_calebfontenot/MapASDV.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package mapasdv_calebfontenot;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
public class MapASDV<K, V> implements Map<K, V>, Cloneable {
private int capacity = 4;
private double = 0.75;
private ArrayList<ListASDV<EntryASDV<K, V>>> map = new ArrayList<>();
private Set<K> sharedKeySet = new SharedSet<>();
private Set<Entry<K, V>> sharedEntrySet = new SharedSet<>();
private Collection<V> sharedValuesCollection = new SharedCollection<>();
private class SharedCollection<V> extends ArrayList<V> {
public boolean addValue(V v) {
return this.add(v);
}
@Override
public boolean remove(Object o) {
return MapASDV.this.removeFirstValue(o);
}
o
callFromOuterClass
public boolean remove(V v, boolean callFromOuterClass) {
boolean b = super.remove(v);
return b;
}
@Override
public Object clone() {
return super.clone();
}
@Override
public void clear() {
super.clear();
}
}
private class SharedSet<E> extends HashSet<E> {
@Override
public boolean add(E e) {
return super.add(e);
}
@Override
public boolean removeAll(Collection<?> c) {
return super.removeAll(c);
}
e
private boolean addEntry(E e) {
return super.add(e);
}
@Override
public boolean remove(Object o) {
if (o instanceof EntryASDV) {
return removeEntry((EntryASDV) o);
}
MapASDV.this.remove(o);
return super.remove(o);
}
entry
private boolean removeEntry(EntryASDV<K, V> entry) {
MapASDV.this.remove(entry.getKey());
return super.remove(entry);
}
o
callFromOuterClass
public boolean remove(E o, boolean callFromOuterClass) {
return super.remove(o);
}
@Override
public Object clone() {
return super.clone();
}
@Override
public void clear() {
super.clear();
}
}
public MapASDV() {
for (int i = 0; i < capacity; ++i) {
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
private void doubleTheSizeOfTheMapAndRehash() {
capacity *= 2;
ArrayList<ListASDV<EntryASDV<K, V>>> newMap = new ArrayList<ListASDV<EntryASDV<K, V>>>();
for (int i = 0; i < capacity; ++i) {
newMap.add(new ListASDV<EntryASDV<K, V>>());
}
for (int i = 0; i < map.size(); ++i)
{
ListASDV<EntryASDV<K, V>> list = map.get(i);
for (int j = 0; j < list.size(); ++j) {
int index = hash(list.get(j).getKey().hashCode());
newMap.get(index).add(list.get(j));
}
}
map = newMap;
}
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K> {
K key;
V value;
public EntryASDV(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString() {
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key)) {
return false;
}
return true;
}
o
@Override
public int compareTo(K o) {
if (getClass() != o.getClass()) {
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}
@Override
public int size() {
return this.keySet().size();
}
@Override
public boolean isEmpty() {
if (map.size() == 0) {
return true;
}
return false;
}
@Override
public boolean containsKey(Object key) {
String dashes = "---------";
System.out.println(dashes + " containsKey(" + key.toString() + ") " + dashes);
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
if (map.get(i).get(j).key.equals(key)) {
return true;
}
}
}
return false;
}
key
private EntryASDV<K, V> getEntryForKey(Object key) {
for (ListASDV<EntryASDV<K, V>> list : map) {
Iterator<EntryASDV<K, V>> it = list.iterator();
while (it.hasNext()) {
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
return entry;
}
}
}
return null;
}
key
private int getIndexForKey(Object key) {
for (int i = 0; i < map.size(); ++i) {
Iterator<EntryASDV<K, V>> it = map.get(i).iterator();
while (it.hasNext()) {
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey())) {
return i;
}
}
}
return -1;
}
value
@Override
public boolean containsValue(Object value) {
String dashes = "---------";
System.out.println(dashes + " containsValue(" + value.toString() + ") " + dashes);
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
if (map.get(i).get(j).value.equals(value)) {
return true;
}
}
}
return false;
}
key
@Override
public V get(Object key) {
String dashes = "---------";
try {
System.out.println(dashes + " get(" + key.toString() + ") " + dashes);
} catch (NullPointerException ex) {
System.out.println(dashes + " get(" + "null" + ") " + dashes);
throw new NullPointerException("null parameter");
}
Object currentKey = null;
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
currentKey = map.get(i).get(j).key;
if (currentKey.equals(key)) {
return map.get(i).get(j).value;
}
}
}
return null;
}
key
value
@Override
public V put(K key, V value) {
if (key == null || value == null) {
throw new NullPointerException("parm(s) null");
}
EntryASDV<K, V> entry = getEntryForKey(key);
if (entry != null) {
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
int code = this.hash(key.hashCode());
int index = hash(code);
ListASDV<EntryASDV<K, V>> list = map.get(index);
EntryASDV e = new EntryASDV(key, value);
list.add(e);
((SharedSet<K>) this.sharedKeySet).addEntry(key);
((SharedSet<Entry<K, V>>) this.sharedEntrySet).addEntry(e);
V v = list.get(list.size() - 1).getValue();
((SharedCollection<V>) this.sharedValuesCollection).addValue(v);
if ((double) this.size() / capacity >= 0.75) {
this.doubleTheSizeOfTheMapAndRehash();
}
return v;
}
int hash(int keyHashCode) {
int h = hashHash(keyHashCode);
return Math.abs(h % capacity - 1);
}
value
private boolean removeFirstValue(Object value) {
Iterator<V> iterator = sharedValuesCollection.iterator();
while (iterator.hasNext()) {
Object o = iterator.next();
if (o.equals(value)) {
iterator.remove();
return true;
}
}
return false;
}
h
private static int hashHash(int h) {
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
public static <T> T cast(Object o, Class<T> clazz) {
return clazz.isInstance(o) ? clazz.cast(o) : null;
}
key
@Override
public V remove(Object key) {
String dashes = "---------";
try {
System.out.println(dashes + " remove(" + key.toString() + ") " + dashes);
} catch (NullPointerException ex) {
System.out.println(ex);
System.out.println(dashes + " remove(" + "null" + ") " + dashes);
throw new NullPointerException("null parameter");
}
K currentKey = null;
V currentValue = null;
EntryASDV<K, V> currentEntry = null;
for (int i = 0; i < map.size(); ++i) {
for (int j = 0; j < map.get(i).size(); ++j) {
currentEntry = map.get(i).get(j);
currentKey = currentEntry.key;
currentValue = currentEntry.value;
if (currentKey.equals(key) || currentValue.equals(key)) {
System.out.println("key: " + currentKey + "; value: " + currentValue);
map.remove(i);
Object[] iterateArray = sharedKeySet.toArray();
for (Object o : iterateArray) {
if (o.equals(currentKey)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedKeySet.remove(currentKey));
break;
}
}
iterateArray = sharedEntrySet.toArray();
for (Object o : iterateArray) {
if (o.equals(currentEntry)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedEntrySet.remove(currentEntry));
break;
}
}
iterateArray = sharedValuesCollection.toArray();
for (Object o : iterateArray) {
if (o.equals(currentValue)) {
System.out.println("removing " + o);
System.out.println("remove successful? " + sharedValuesCollection.remove(o));
break;
}
}
return currentValue;
}
}
}
return null;
}
m
@Override
public void putAll(Map<? extends K, ? extends V> m) {
if (m == null) {
throw new NullPointerException("null parameter");
}
BiConsumer consumeEachEntry = new BiConsumer<K, V>() {
MapASDV<K, V> mapForConsumer = MapASDV.this;
@Override
public void accept(K k, V v) {
mapForConsumer.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
@Override
public void clear() {
map = new ArrayList<>();
sharedKeySet = new SharedSet<K>();
sharedEntrySet = new SharedSet<Entry<K, V>>();
sharedValuesCollection = new SharedCollection<V>();
for (int i = 0; i < capacity; ++i) {
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
@Override
public Set<K> keySet() {
return this.sharedKeySet;
}
@Override
public Collection<V> values() {
return sharedValuesCollection;
}
@Override
public Set<Entry<K, V>> entrySet() {
return this.sharedEntrySet;
}
@Override
public String toString() {
String s = "[ ";
for (int i = 0; i < map.size(); ++i) {
s += map.get(i).toString() + "\n";
}
s += " ]";
return s;
}
@Override
public Object clone() {
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
clonedMap.putAll(this);
return clonedMap;
}
public static void main(String[] args) {
MapASDV<String, Integer> map = new MapASDV<>();
System.out.println("---------------------------testing put(K, V)");
map.put("ann", 20);
map.put("coco", 25);
System.out.println(map);
MapASDV<String, Integer> clonedMap = (MapASDV<String, Integer>) map.clone();
System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)");
map.put("Jonathan", 30);
System.out.println(map);
map.put("Jonhathan", 45);
System.out.println(map);
map.put("Alexander", 33);
System.out.println(map);
System.out.println("\n\n---------------------------testing putAll(Map<K,V>)");
Map<String, Integer> anotherJavaMap = new HashMap();
anotherJavaMap.put("lion king", 45);
anotherJavaMap.put("HYENA", 6);
map.putAll(anotherJavaMap);
System.out.println(map);
System.out.println("\n\n---------------------------testing containsKey");
System.out.println(map.containsKey("Alexander"));
System.out.println(map.containsKey("alexander"));
System.out.println("\n\n---------------------------testing containsValue");
System.out.println(map.containsValue(33));
System.out.println(map.containsValue(34));
System.out.println("\n\n---------------------------testing getEntryForKey");
Entry<String, Integer> e = map.getEntryForKey("Alexander");
System.out.println(map.getEntryForKey("Alexander"));
System.out.println(map.getEntryForKey("Alex"));
System.out.println("\n\n---------------------------testing get");
System.out.println(map.get("Alexander"));
System.out.println(map.get("Alex"));
try {
map.get(null);
} catch (NullPointerException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\n\n---------------------------testing getIndexForKey");
System.out.println(map.getIndexForKey("Alexander"));
System.out.println(map.getIndexForKey("Alex"));
System.out.println("\n\n---------------------------testing isEmpty");
System.out.println(map.isEmpty());
System.out.println("\n\n---------------------------testing size");
System.out.println(map.size());
System.out.println(map);
System.out.println("\n\n---------------------------testing entrySet()");
Set<Entry<String, Integer>> entries = map.entrySet();
System.out.println(entries);
System.out.println("\n\n---------------------------testing keySet()");
Set<String> keys = map.keySet();
System.out.println(keys);
System.out.println("\n\n---------------------------testing values()");
Collection<Integer> values = map.values();
System.out.println(values);
System.out.println("\n\n---------------------------testing remove( K) coco 25");
map.remove("coco");
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Entry-Collection remove ");
entries.remove(e);
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Set Keys remove ");
keys.remove("ann");
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing Set Values remove ");
values.remove(45);
values = map.values();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing clear ");
map.clear();
values = map.values();
entries = map.entrySet();
keys = map.keySet();
System.out.println("map: " + map);
System.out.println("entries: " + entries);
System.out.println("keys: " + keys);
System.out.println("values: " + values);
System.out.println("\n\n---------------------------testing add of sets and collections ");
try {
keys.add("a");
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
values.add(33);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
entries.add(e);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
System.out.println("\n\n---------------------------testing clone");
System.out.println(clonedMap);
System.out.println("---------------------------testing put(K, V) AGAIN");
map.put("Nicholas", 100);
map.put("a", 200);
map.put("b", -20);
System.out.println(map);
}
}