tdudzik

HashMap

Sep 9th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. interface Map<K, V> {
  2.  
  3.     void put(K key, V value);
  4.  
  5.     V get(K key);
  6.  
  7.     boolean containsKey(K key);
  8.  
  9.     interface Entry<K, V> {
  10.  
  11.         K getKey();
  12.  
  13.         V getValue();
  14.  
  15.         Entry<K, V> getNext();
  16.  
  17.     }
  18.  
  19. }
  20.  
  21. class HashMap<K, V> implements Map<K, V> {
  22.  
  23.     private final static int DEFAULT_CAPACITY = 16;
  24.  
  25.     private Node<K, V>[] nodes;
  26.  
  27.     @SuppressWarnings("unchecked")
  28.     public HashMap() {
  29.         nodes = (Node<K, V>[]) new Node[DEFAULT_CAPACITY];
  30.     }
  31.  
  32.     @Override
  33.     public void put(K key, V value) {
  34.         int position = hash(key);
  35.  
  36.         Node<K, V> node = nodes[position];
  37.         if (node == null) {
  38.             nodes[position] = new Node<>(key, value, null);
  39.         } else {
  40.             while (node.getNext() != null) {
  41.                 node = node.getNext();
  42.             }
  43.             node.next = new Node<>(key, value, null);
  44.         }
  45.     }
  46.  
  47.     @Override
  48.     public V get(K key) {
  49.         int position = hash(key);
  50.         Node<K, V> node = nodes[position];
  51.  
  52.         if (node == null) {
  53.             return null;
  54.         }
  55.  
  56.         while (!node.getKey().equals(key)) {
  57.             node = node.getNext();
  58.  
  59.             if (node == null) {
  60.                 return null;
  61.             }
  62.         }
  63.  
  64.         return node.getValue();
  65.     }
  66.  
  67.     @Override
  68.     public boolean containsKey(K key) {
  69.         int position = hash(key);
  70.         Node<K, V> node = nodes[position];
  71.  
  72.         if (node == null) {
  73.             return false;
  74.         }
  75.  
  76.         while (!node.getKey().equals(key)) {
  77.             node = node.getNext();
  78.  
  79.             if (node == null) {
  80.                 return false;
  81.             }
  82.         }
  83.  
  84.         return true;
  85.     }
  86.  
  87.     private int hash(K key) {
  88.         return key == null ? 0 : key.hashCode() % nodes.length;
  89.     }
  90.  
  91.     private static class Node<K, V> implements Map.Entry<K, V> {
  92.  
  93.         private final K key;
  94.         private V value;
  95.         private Node<K, V> next;
  96.  
  97.         public Node(K key, V value, Node<K, V> next) {
  98.             this.key = key;
  99.             this.value = value;
  100.  
  101.         }
  102.  
  103.         @Override
  104.         public K getKey() {
  105.             return key;
  106.         }
  107.  
  108.         @Override
  109.         public V getValue() {
  110.             return value;
  111.         }
  112.  
  113.         @Override
  114.         public Node<K, V> getNext() {
  115.             return next;
  116.         }
  117.  
  118.     }
  119.  
  120. }
  121.  
  122. public class Application {
  123.  
  124.     public static void main(String[] args) {
  125.         Map<String, Integer> numbersByString = new HashMap<>();
  126.         numbersByString.put("1", 1);
  127.         numbersByString.put("2", 2);
  128.         numbersByString.put("3", 3);
  129.  
  130.         System.out.println(numbersByString.get("1").equals(1));
  131.         System.out.println(numbersByString.get("2").equals(2));
  132.         System.out.println(numbersByString.get("3").equals(3));
  133.         System.out.println(numbersByString.get("4") == null);
  134.  
  135.         System.out.println(numbersByString.containsKey("1") == true);
  136.         System.out.println(numbersByString.containsKey("2") == true);
  137.         System.out.println(numbersByString.containsKey("3") == true);
  138.         System.out.println(numbersByString.containsKey("4") == false);
  139.     }
  140.  
  141. }
Add Comment
Please, Sign In to add comment