Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package map;
- import java.util.ArrayList;
- import java.util.HashSet;
- public class SimpleHashMap<K,V> implements Map<K,V> {
- int size;
- int Capacity;
- private Entry<K,V>[] E;
- public SimpleHashMap(){
- E = (Entry<K,V>[]) new Entry[16];
- Capacity = 16;
- size = 0;
- }
- public SimpleHashMap(int Capacity){
- E = (Entry<K,V>[]) new Entry[Capacity];
- size = 0;
- this.Capacity=Capacity;
- }
- @Override
- public V get(K key) {
- int index = index(key);
- if(index == -1) {
- return null;
- } else {
- return find(index(key),key).value;
- }
- }
- @Override
- public boolean isEmpty() {
- return size == 0;
- }
- @Override
- public V put(K arg0, V arg1) {
- if(index(arg0) == -1) {
- int i = Math.abs(arg0.hashCode()%Capacity);
- if(E[i]==null) {
- E[i] = new Entry<K, V>(arg0,arg1);
- } else {
- Entry<K,V> inputChain = E[i];
- while(inputChain.next!=null) {
- inputChain=inputChain.next;
- }
- inputChain.next = new Entry<K, V>(arg0,arg1);
- }
- if((size*100)/E.length > 75) {
- System.out.println("rehash");
- rehash();
- }
- size++;
- return null;
- }
- V vOld = find(index(arg0),arg0).value;
- find(index(arg0),arg0).setValue(arg1);
- return vOld;
- }
- @Override
- public V remove(Object arg0) {
- size--;
- return null;
- }
- @Override
- public int size() {
- return size;
- }
- public String show() {
- StringBuilder sb = new StringBuilder();
- for(int i=0; i<E.length; i++) {
- if(E[i] != null) {
- sb.append(i+" "+E[i].toString());
- Entry<K,V> printEntry = E[i];
- while(printEntry.next != null) {
- printEntry = printEntry.next;
- sb.append(" " + printEntry.toString());
- }
- sb.append("\n");
- }
- }
- return sb.toString();
- }
- private int index(K key) {
- int index =0;
- for(Entry<K,V> e: E) {
- if(e != null) {
- if(e.getKey().equals(key)) {
- return index;
- }
- Entry<K,V> currChain = e;
- while(currChain.next!= null) {
- currChain = currChain.next;
- if(currChain.getKey().equals(key)) {
- return index;
- }
- }
- }
- index++;
- }
- return -1;
- }
- private Entry<K,V> find(int index, K key){
- if(E[index]!=null) {
- Entry<K,V> currEntry = E[index];
- while(currEntry.next!=null) {
- if(currEntry.getKey().equals(key)) {
- return currEntry;
- }
- currEntry = currEntry.next;
- }
- return currEntry;
- }
- return null;
- }
- private void rehash(){
- ArrayList<Entry<K,V>> allEntries = new ArrayList<Entry<K,V>>();
- for(Entry<K,V> e : E) {
- if(e!=null) {
- allEntries.add(e);
- while(e.next!=null) {
- e=e.next;
- allEntries.add(e);
- }
- }
- }
- System.out.println(allEntries.size());
- Capacity = Capacity *2;
- size = 0;
- E = (Entry<K,V>[]) new Entry[Capacity];
- for(Entry<K,V> e : allEntries) {
- put(e.getKey(),e.getValue());
- }
- }
- private static class Entry<K,V> implements Map.Entry<K, V> {
- private K key;
- private V value;
- private Entry<K,V> next;
- public Entry(K key, V value){
- this.key=key;
- this.value=value;
- next = null;
- }
- @Override
- public K getKey() {
- return key;
- }
- @Override
- public V getValue() {
- return value;
- }
- @Override
- public V setValue(V value) {
- this.value=value;
- return value;
- }
- public String toString() {
- return key + "=" + value;
- }
- }
- public static void main(String[] args) {
- // SimpleHashMap<String, Integer> SHM = new SimpleHashMap<String, Integer>();
- // SHM.put("ett", 1);
- // SHM.put("två", 2);
- // SHM.put("tre", 3);
- // SHM.put("fyra", 4);
- // SHM.put("fem", 5);
- // SHM.put("sex", 6);
- // SHM.put("sju", 7);
- // SHM.put("åtta", 8);
- // SHM.put("nio", 9);
- // SHM.put("tio", 10);
- // SHM.put("elva", 11);
- // SHM.put("tolv", 12);
- // SHM.put("treton", 13);
- // SHM.put("fjorton", 14);
- // SHM.put("femton", 15);
- // SHM.put("sexton", 17);
- // SHM.put("sjuton", 2);
- // SHM.put("arton", 2);
- // SHM.put("niton", 2);
- // SHM.put("tjugo", 2);
- // SHM.put("tjugoett", 2);
- // SHM.put("tjugotvå", 2);
- //
- // System.out.println(SHM.show());
- // System.out.println(SHM.get("två"));
- SimpleHashMap<Integer,Integer> hashmap = new SimpleHashMap<Integer,Integer>();
- for(int i=0; i<100; i++) {
- hashmap.put(i, i);
- // System.out.println(hashmap.show());
- }
- SimpleHashMap<Integer,Integer> m = new SimpleHashMap<Integer,Integer>();
- for(int i=0; i<16; i++) {
- m.put(i, i);
- System.out.println(m.size +" \n"+ m.show());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement