NicholasAdamou

Counter

Oct 3rd, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.39 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class Counter<T> {
  4.     private final HashMap<T, Integer> map = new HashMap<T, Integer>();
  5.  
  6.     public int get(T key) {
  7.         final Integer n = map.get(key);
  8.         return n == null ? 0 : n;
  9.     }
  10.  
  11.     public void count(T key) {
  12.         map.put(key, get(key) + 1);
  13.     }
  14.    
  15.     public HashMap<T, Integer> getMap() {
  16.         return map;
  17.     }
  18. }
Add Comment
Please, Sign In to add comment