0

Adding on the discussion: What is the significance of load factor in HashMap?

If i am using org.apache.commons.collections.map.LRUMap will the size of the LRUMap increase once it reaches the max size.

If i instantiated LRUMap

        maxSize= 1024
       LRUMap myCacheMap = new LRUMap(maxSize)

if the myCacheMap reaches the 1024th key value pair or entry and if i added another element will it increase to 2048 automatically. If it increases automatically, then why do we have the isFull() method for it. As it will never be Full it will automatically increase. Am I missing a point in this. Sorry for asking two questions in one.

Thanks in advance.

Community
  • 1
  • 1
sarmahdi
  • 1,098
  • 4
  • 21
  • 61

2 Answers2

2

The point of LRUMap is that it has a fixed maximum size, which doesn't increase automatically when reached. A new entry added when the map is full causes the removal of the least recently used entry.

A Map implementation with a fixed maximum size which removes the least recently used entry if an entry is added when full.

Therefore isFull() makes sense. If it returns true, you know that adding a new entry to the map will remove the least recently used entry.

Eran
  • 387,369
  • 54
  • 702
  • 768
2

Max size will never be increased. That is the basic principle of any least recently used (LRU) algorithm based map.

when the LRUMap reaches its maximum size, it just removes the least recently used entry from the map.

Here is the snippet from the source of LRUMap.put()

if ( mapSize >= maximumSize ) {

        // don't retire LRU if you are just
        // updating an existing key
        if (!containsKey(key)) {
            // lets retire the least recently used item in the cache
           removeLRU();
        }
    }
Kvk
  • 522
  • 5
  • 15
  • thanks to both of your answer. Just one more thing. Then why is there a load factor in LRUMap. I actually also thought that as we are saying there is a MaxSize and there is a isFull() method, the size should not increase as it is LRU. Then I saw the linked discussion on load factor and hence my question. Does load factor play any role in LRU then. – sarmahdi Aug 03 '14 at 05:09
  • While creating LRUMap, if you specify the maxsize as < 1, map intially gets created with DEFAULT_CAPACITY(16). From there onwards, it can be increased up to DEFAULT_MAX_SIZE (100). load factor and thresh hold are used in this scenario. Once the map reaches max size, it will never be increased. – Kvk Aug 03 '14 at 12:47