This class wraps a HashMap and provides methods by which key objects can be associated with "counting" values. : HashMap « Collections Data Structure « Java






This class wraps a HashMap and provides methods by which key objects can be associated with "counting" values.

    
//package org.groovymud.utils;

/* Copyright 2008 Matthew Corby-Eaglen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0 
*
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * This class wraps a HashMap and provides methods by which key
 * objects can be associated with "counting" values.
 * 
 * useful for timers or other such counting effects
 * 
 * e.g 
 * countingMap.put(object); // puts (object, 0) into the map
 * countingMap.increment(object) // adds 1 to the value of the object (object, 1)
 * countingMap.decrement(object) // decreases the value at the object's key reference by 1 (object, 0)
 * @author matt
 *
 */
public class CountingMap {

  Map<Object, Integer> backingMap = new HashMap<Object, Integer>();

  public void clear() {
    backingMap.clear();
  }

  public boolean containsKey(Object key) {
    return backingMap.containsKey(key);
  }

  public boolean containsValue(Object value) {
    return backingMap.containsValue(value);
  }

  public Set<?> entrySet() {
    return backingMap.entrySet();
  }

  public boolean equals(Object o) {
    return backingMap.equals(o);
  }

  public Integer get(Object key) {
    return backingMap.get(key);
  }

  public int hashCode() {
    return backingMap.hashCode();
  }

  public boolean isEmpty() {
    return backingMap.isEmpty();
  }

  public Set<?> keySet() {
    return backingMap.keySet();
  }

  public Integer remove(Object key) {
    return backingMap.remove(key);
  }

  public int size() {
    return backingMap.size();
  }

  public Collection<Integer> values() {
    return backingMap.values();
  }

  public Integer put(Object key) {
    return put(key, new Integer(0));
  }

  public Integer put(Object key, Integer value) {
    return backingMap.put(key, value);
  }

  public void putAll(Map<Object, Integer> m) {
    backingMap.putAll(m);
  }

  public void increment(Object key) {
    if (!backingMap.containsKey(key)) {
      backingMap.put(key, new Integer(0));
    } else {
      Integer i = backingMap.get(key);
      backingMap.put(key, new Integer(i.intValue() + 1));
    }
  }

  public void decrement(Object key) {
    if (!backingMap.containsKey(key)) {
      backingMap.put(key, new Integer(0));
    } else {
      Integer i = backingMap.get(key);
      backingMap.put(key, new Integer(i.intValue() - 1));
    }
  }

  public Set getKeysAt(int limit) {
    Set ret = new HashSet<Object>();
    for (Object obj : backingMap.keySet()) {
      if (backingMap.get(obj).intValue() == limit) {
        ret.add(obj);
      }
    }
    return ret;
  }

  public Set getKeysAbove(int limit) {
    Set ret = new HashSet<Object>();
    for (Object obj : backingMap.keySet()) {
      if (backingMap.get(obj).intValue() > limit) {
        ret.add(obj);
      }
    }
    return ret;
  }

  public Set getKeysBelow(int limit) {
    Set ret = new HashSet<Object>();
    for (Object obj : backingMap.keySet()) {
      if (backingMap.get(obj).intValue() < limit) {
        ret.add(obj);
      }
    }
    return ret;
  }

}

   
    
    
    
  








Related examples in the same category

1.Iterate through the values of Java HashMap example
2.Get Synchronized Map from Java HashMap example
3.Check if a particular key exists in Java HashMap example
4.Check if a particular value exists in Java HashMap example
5.Get Set view of Keys from Java HashMap example
6.Get Size of Java HashMap
7.Remove all values from Java HashMap example
8.Remove value from Java HashMap
9.Create Java Hashtable from HashMap
10.Sort an HashMap based on the keys
11.For keys of a map
12.For values of a map
13.For both the keys and values of a map
14.Storing Primitive Types in a Collection
15.Creating a Copy of a Collection: the objects are not cloned.
16.Use Iterator to loop through the map key set
17.Generic hashmap with String as key and Integer as valueGeneric hashmap with String as key and Integer as value
18.Get key set and value set from map and use Iterator to loop through them
19.Clones a map
20.Hash map for counting references to Object keys.
21.Extended Version of java.util.HashMap that provides an extended get method accpeting a default value.
22.Compact HashMap
23.This extension of HashMap support duplicate keys
24.Concurrent Hopscotch HashMap