edd99881d7
Also removed Preferences appendix
65 lines
1.7 KiB
Java
65 lines
1.7 KiB
Java
// equalshashcode/SlowMap.java
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// A Map implemented with ArrayLists
|
|
import java.util.*;
|
|
import onjava.*;
|
|
|
|
public class SlowMap<K, V> extends AbstractMap<K, V> {
|
|
private List<K> keys = new ArrayList<>();
|
|
private List<V> values = new ArrayList<>();
|
|
@Override
|
|
public V put(K key, V value) {
|
|
V oldValue = get(key); // The old value or null
|
|
if(!keys.contains(key)) {
|
|
keys.add(key);
|
|
values.add(value);
|
|
} else
|
|
values.set(keys.indexOf(key), value);
|
|
return oldValue;
|
|
}
|
|
@Override
|
|
public V get(Object key) { // key: type Object, not K
|
|
if(!keys.contains(key))
|
|
return null;
|
|
return values.get(keys.indexOf(key));
|
|
}
|
|
@Override
|
|
public Set<Map.Entry<K, V>> entrySet() {
|
|
Set<Map.Entry<K, V>> set= new HashSet<>();
|
|
Iterator<K> ki = keys.iterator();
|
|
Iterator<V> vi = values.iterator();
|
|
while(ki.hasNext())
|
|
set.add(new MapEntry<>(ki.next(), vi.next()));
|
|
return set;
|
|
}
|
|
public static void main(String[] args) {
|
|
SlowMap<String,String> m= new SlowMap<>();
|
|
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);
|
|
}
|
|
}
|
|
/* Output:
|
|
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
|
|
*/
|