OnJava8-Examples/equalshashcode/SimpleHashMap.java

91 lines
2.4 KiB
Java
Raw Normal View History

2017-01-10 14:11:16 -08:00
// equalshashcode/SimpleHashMap.java
2016-12-30 17:23:13 -08:00
// (c)2017 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2016-01-25 18:05:55 -08:00
// A demonstration hashed Map
2015-06-15 17:47:35 -07:00
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
2016-01-25 18:05:55 -08:00
public
class SimpleHashMap<K, V> extends AbstractMap<K, V> {
2015-06-15 17:47:35 -07:00
// Choose a prime number for the hash table
// size, to achieve a uniform distribution:
static final int SIZE = 997;
// You can't have a physical array of generics,
// but you can upcast to one:
@SuppressWarnings("unchecked")
2015-09-07 11:44:36 -06:00
LinkedList<MapEntry<K, V>>[] buckets =
2015-06-15 17:47:35 -07:00
new LinkedList[SIZE];
@Override
public V put(K key, V value) {
V oldValue = null;
int index = Math.abs(key.hashCode()) % SIZE;
if(buckets[index] == null)
buckets[index] = new LinkedList<>();
2015-09-07 11:44:36 -06:00
LinkedList<MapEntry<K, V>> bucket = buckets[index];
MapEntry<K, V> pair = new MapEntry<>(key, value);
2015-06-15 17:47:35 -07:00
boolean found = false;
2016-01-25 18:05:55 -08:00
ListIterator<MapEntry<K, V>> it =
bucket.listIterator();
2015-06-15 17:47:35 -07:00
while(it.hasNext()) {
2015-09-07 11:44:36 -06:00
MapEntry<K, V> iPair = it.next();
2015-06-15 17:47:35 -07:00
if(iPair.getKey().equals(key)) {
oldValue = iPair.getValue();
it.set(pair); // Replace old with new
found = true;
break;
}
}
if(!found)
buckets[index].add(pair);
return oldValue;
}
@Override
public V get(Object key) {
int index = Math.abs(key.hashCode()) % SIZE;
if(buckets[index] == null) return null;
2015-09-07 11:44:36 -06:00
for(MapEntry<K, V> iPair : buckets[index])
2015-06-15 17:47:35 -07:00
if(iPair.getKey().equals(key))
return iPair.getValue();
return null;
}
@Override
2015-09-07 11:44:36 -06:00
public Set<Map.Entry<K, V>> entrySet() {
Set<Map.Entry<K, V>> set= new HashSet<>();
for(LinkedList<MapEntry<K, V>> bucket : buckets) {
2015-06-15 17:47:35 -07:00
if(bucket == null) continue;
2015-09-07 11:44:36 -06:00
for(MapEntry<K, V> mpair : bucket)
2015-06-15 17:47:35 -07:00
set.add(mpair);
}
return set;
}
public static void main(String[] args) {
2016-01-25 18:05:55 -08:00
SimpleHashMap<String,String> m =
new SimpleHashMap<>();
2017-01-10 14:11:16 -08:00
m.putAll(Countries.capitals(8));
m.forEach((k, v) ->
System.out.println(k + "=" + v));
System.out.println(m.get("BENIN"));
m.entrySet().forEach(System.out::println);
2015-06-15 17:47:35 -07:00
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2017-01-10 14:11:16 -08:00
CAMEROON=Yaounde
ANGOLA=Luanda
BURKINA FASO=Ouagadougou
BURUNDI=Bujumbura
ALGERIA=Algiers
BENIN=Porto-Novo
CAPE VERDE=Praia
BOTSWANA=Gaberone
Porto-Novo
CAMEROON=Yaounde
ANGOLA=Luanda
BURKINA FASO=Ouagadougou
BURUNDI=Bujumbura
ALGERIA=Algiers
BENIN=Porto-Novo
CAPE VERDE=Praia
BOTSWANA=Gaberone
2015-09-07 11:44:36 -06:00
*/