vivekjustthink / WeakConcurrentHashMap

Contains WeakConcurrentHashMap Java Implementation
18 stars 7 forks source link

WeakConcurrentHashMap

A Weak Concurrent Hash Map Solution which stores the keys and values only for a specific amount of time, and then expires after that time.


  // Create a Map Object
  long expiryInMillis = 1 * 60 * 1000;  // 1 minute
  WeakConcurrentHashMap<String, Long> map = new WeakConcurrentHashMap<String, Long>(expiryInMillis);

  // Use it
  map.put("key", valueObject);
  Long valueObject = map.get("key");

  // quit using it
  map.quitMap();
  

And to check if the map is alive

  if (map.isAlive()) {
    // Your operations on map
  }
  

Listener Implementation

class MyMapListener implements WeakConcurrentHashMapListener {

    @Override
    public void notifyOnAdd(String key, Long value) {
        System.out.println("New key added to map. Key : " + key + ", Value : " + value);
    }

    @Override
    public void notifyOnRemoval(String key, Long value) {
        RestLogger.info("Key Removed from Map Key: " + key + ", Value : " + value);
    }
}

@author Vivekananthan M (vivekjustthink@gmail.com)