Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package map;
- public class SimpleHashMap<K, V> implements Map<K, V> {
- private Entry<K, V>[] table;
- private double loadFactor;
- private int size;
- public SimpleHashMap() {
- table = (Entry<K, V>[]) new Entry[16];
- loadFactor = 0.75;
- size = 0;
- }
- public SimpleHashMap(int capacity) {
- table = (Entry<K, V>[]) new Entry[capacity];
- loadFactor = 0.75;
- }
- public String show() {
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < table.length; i++) {
- sb.append(i + "\t");
- Entry<K, V> temp = table[i];
- while (temp != null) {
- sb.append(temp.toString());
- // Behöver ej fungera
- temp = (Entry<K, V>) temp.next;
- }
- sb.append("\n");
- }
- return sb.toString();
- }
- @Override
- public V get(Object arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public boolean isEmpty() {
- for (int i = 0; i < table.length; i++) {
- if (table[i] != null) {
- return false;
- }
- }
- return true;
- }
- @Override
- public V put(K arg0, V arg1) {
- size++;
- if((double)size/(double)table.length>0.75){
- rehash();
- }
- if(table[index(arg0)]==null){
- Entry<K,V> neu = new Entry(arg0,arg1);
- table[index(arg0)]= neu;
- return null;
- }
- Entry<K,V> temp = table[index(arg0)];
- while(temp!=null){
- if(temp.key.equals(arg0)){
- V tempv = temp.value;
- temp.value=arg1;
- return tempv;
- }else if (temp.next==null){
- temp = table[index(arg0)];
- table[index(arg0)]= new Entry(arg0,arg1);
- table[index(arg0)].next = (Entry<K, V>) temp;
- return null;
- }
- temp = (Entry<K, V>) temp.next;
- }
- return null;
- }
- private void rehash(){
- SimpleHashMap s = new SimpleHashMap(2*table.length);
- for (int i = 0; i < table.length; i++) {
- Entry<K,V> temp = table[i];
- while(temp!=null){
- s.put(temp.key, temp.value);
- temp = (Entry<K, V>) temp.next;
- }
- }
- table = s.table;
- }
- @Override
- public V remove(Object arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public int size() {
- return size;
- }
- private int index(K key) {
- int index = key.hashCode() % table.length;
- // Math.abs används inte då det kan returnera negativa värden
- if (index < 0) {
- index = +table.length;
- }
- return index;
- }
- private Entry<K, V> find(int index, K key) {
- Entry<K, V> temp = table[index];
- while (temp != null) {
- if (temp.key.equals(key)) {
- return temp;
- }
- // Behöver fungera
- temp = (Entry<K, V>) temp.next;
- }
- return null;
- /*
- * while(temp!=null){ if(temp.equals(key)){ return temp; } temp =
- * (Entry<K, V>) temp.next; } return null;
- */
- }
- 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 k, V v) {
- value = v;
- key = k;
- }
- @Override
- public V get(Object arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public boolean isEmpty() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public V put(K arg0, V arg1) {
- Entry<K, V> neu = new Entry(arg0, arg1);
- return null;
- }
- @Override
- public V remove(Object arg0) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public int size() {
- // TODO Auto-generated method stub
- return 0;
- }
- public String toString() {
- return key + "=" + value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement