Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hash;
- import java.util.LinkedList;
- import java.util.ListIterator;
- import list.ListNode;
- /**
- * Hash Table implementation.
- *
- */
- public class HashTable<K,V> implements Map<K,V>{
- private static final int INITIAL_CAPACITY = 100;
- private int size = 0;
- /* Max allowed load factor */
- private double maxLoad = 0.75;
- /* Sentinel value for removes */
- private HashNode<K,V> sentinel = new HashNode<K,V>(null, null);
- private LinkedList table[];
- ListIterator <HashNode>nodeIterator;
- public HashTable() {
- this(INITIAL_CAPACITY);
- }
- double maximum = maxLoad;
- public HashTable(int initialCapacity) {
- //TODO: Write this method
- //any information you want about the hash table
- table = new LinkedList[initialCapacity];
- size = 0;
- double maximum = maxLoad;
- //for(int i = 0; i<table.length; i++) {
- //table[i] = new LinkedList <HashNode> ();
- //}
- //to figure out index of array, compute hash
- for(int i = 0; i<table.length; i++) {
- if(table[i]==null) {
- //create new linked list
- table[i] = new LinkedList<>();
- }
- }
- if(size>0) {
- double loadFactor = (double)size/table.length;
- }
- }
- @Override
- public void insert(K key, V value) {
- //TODO: Write this method
- //to figure out index of array, compute hash
- //insert linked list at that index
- //size goes up with each value added to hash table
- if(size>0) {
- double loadFactor = (double)size/table.length;
- int nodeNumber = (Math.abs(key.hashCode()))%(table.length);
- ListIterator <HashNode> nodeIterator = table[nodeNumber].listIterator();
- if(loadFactor>=maximum) {
- this.resize();
- }
- for(int i = 0; i<table.length; i++) {
- if(table[nodeNumber]==null) {
- //create new linked list if null
- table[nodeNumber] = new LinkedList<>();
- table[nodeNumber].add(new HashNode(key,value));
- size++;
- }
- }
- //otherwise add value
- while(table[nodeNumber]!=null) {
- //if key is already there, ignore
- if(nodeIterator.next().getKey().equals(key)) {
- }
- table[nodeNumber].add(new HashNode(key,value));
- size++;
- }
- //else {
- //}
- }
- else {
- double loadFactor=0;
- int nodeNumber = (Math.abs(key.hashCode()))%(table.length);
- nodeIterator = table[nodeNumber].listIterator();
- //otherwise add value
- table[nodeNumber].add(new HashNode(key,value));
- size++;
- //else {
- //}
- }
- }
- @Override
- public V retrieve(K key) {
- //TODO: Write this method
- int hashValue =Math.abs(key.hashCode())%(table.length);
- HashNode<K,V> listNode;
- ListIterator <HashNode> nodeIterator = table[hashValue].listIterator();
- while(nodeIterator.hasNext()) {
- if(nodeIterator.next().getKey().equals(key)) {
- return (V) nodeIterator.next().getValue();
- }
- }
- //table[hashValue].forEach(listNode.getKey());
- //ListNode<T> newNode = new ListNode <T>(data);
- // for(int i = 0; i <= hashValue ;i++) {
- // if(listNode.getKey().equals(key)){
- // return (V) listNode.getValue();
- // }
- // }
- return null;
- }
- @Override
- public boolean contains(K key) {
- //TODO: Write this method
- int hashValue =(Math.abs(key.hashCode()))%(table.length);
- if(table[hashValue].listIterator().hasNext()==false){
- return false;
- }
- else;
- return true;
- }
- @Override
- public void remove(K key) {
- //TODO: Write this method
- }
- private void resize() {
- //TODO: Write this method
- }
- /**
- * Getting and setting the maxLoad field
- * @return
- */
- public double getMaxLoad() {return maxLoad;}
- public void setMaxLoad(double maxLoad) {this.maxLoad = maxLoad;}
Add Comment
Please, Sign In to add comment