92 lines
3.1 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/SimpleHashMap.java
// (c)2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ 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<>();
2015-06-15 17:47:35 -07:00
m.putAll(Countries.capitals(25));
System.out.println(m);
System.out.println(m.get("ERITREA"));
System.out.println(m.entrySet());
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
{CAPE VERDE=Praia, ANGOLA=Luanda, ETHIOPIA=Addis Ababa,
BENIN=Porto-Novo, CONGO=Brazzaville, LESOTHO=Maseru,
CENTRAL AFRICAN REPUBLIC=Bangui, EQUATORIAL GUINEA=Malabo,
ERITREA=Asmara, COMOROS=Moroni, BURKINA FASO=Ouagadougou,
GABON=Libreville, THE GAMBIA=Banjul, GUINEA=Conakry,
EGYPT=Cairo, BURUNDI=Bujumbura, ALGERIA=Algiers,
CAMEROON=Yaounde, GHANA=Accra, KENYA=Nairobi, COTE D'IVOIR
(IVORY COAST)=Yamoussoukro, BISSAU=Bissau,
DJIBOUTI=Dijibouti, CHAD=N'djamena, BOTSWANA=Gaberone}
Asmara
[CAPE VERDE=Praia, ANGOLA=Luanda, ETHIOPIA=Addis Ababa,
BENIN=Porto-Novo, CONGO=Brazzaville, LESOTHO=Maseru,
CENTRAL AFRICAN REPUBLIC=Bangui, EQUATORIAL GUINEA=Malabo,
ERITREA=Asmara, COMOROS=Moroni, BURKINA FASO=Ouagadougou,
GABON=Libreville, THE GAMBIA=Banjul, GUINEA=Conakry,
EGYPT=Cairo, BURUNDI=Bujumbura, ALGERIA=Algiers,
CAMEROON=Yaounde, GHANA=Accra, KENYA=Nairobi, COTE D'IVOIR
(IVORY COAST)=Yamoussoukro, BISSAU=Bissau,
DJIBOUTI=Dijibouti, CHAD=N'djamena, BOTSWANA=Gaberone]
2015-09-07 11:44:36 -06:00
*/