Java Map Interface Examples

Recently I was explaining someone about Java Map and various implementations that are available as part of JDK.

I had wrote a simple program to explain him that. Thought of putting it here. Below it is –

TreeMap – The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. Below example you will notice that treeMap toString shows that entries are ordered by performing natural ordering on Int keys.

HashMap – This class makes no guarantees as to the order of the map. Uses object equality to check if key already exist in map and replace value if it exist. As in example while trying to put key11 as it equals with key1 value “One” is replaced with “Eleven”.

IdentityHashMap – Unlike above HashMap, this uses reference-equality in place of object-equality when comparing keys. As in following example even after key11 equals key1, but as their references are not equals so a new key/value is inserted.

LinkedHashMap – This implementation keeps track of order in which keys were inserted into the map. In below example you will notice that the toString shows entries in same order in which they were inserted. This also has another feature discussed next.

LRU Caching using LinkedHashMap – Another feature of LinkedHashMap is that it provide constructor to create a linked hash map whose order of iteration is the order in which its entries were last accessed, from least-recently accessed to most-recently (access-order). In following example, you will notice that when elements are inserted and none is accessed yet, the order of Entries is same as that of above example. But after accessing elements, you will notice that the entries order is changed based on when last it was accessed. Using this feature one can create a LRU(accessed) cache as shown in example. You will need to override removeEldestEntry method of LinkedHashMap and add your logic for caching. Each put operation will call this removeEldestEntry by passing least-recently-accessed element to it and based on removeEldestEntry’s return value it will either be kept or removed from the map.

WeakHashMap– In this implementation keys are held as WeakReferences. This way if there is no strong reference for key, on next Garbage Collection this key and corresponding value will be removed from the Map. Read this article for details on Java References. As in below example we are adding three keys to map and then clearing one strong reference to key6 by setting it to null. Then we request GC by calling System.gc() and in next statement you will notice that the key/value for key6 is removed from Map.

Click to continue reading “Java Map Interface Examples”


Timesjobs and SPAM


Neha Singh
Head, Customer Care (in collaboration with


I really hate websites which even after unsubscribing from  email subscriptions still keep sending you emails.

This time it is yours truly and your new venture

I must have unsubscribed three to four times from the link that you have provided at the end of your marketing emails. I still continue to receive mails.

You dumbs  can’t even spell,

Fresher Jobs on

Can you pls tell me what it would take for you to remove my email address from your system. Heck I don’t want account at your website. Is there any way I can close my account.

Seems TimeJobs  don’t give a damn about user privacy….


Publishing Webservice on Multiple Network Interfaces

Recently our IT support group decided to move one of component to a new VCS Cluster.

This component is a service component exposing various web services which are consumed by another .Net based UI component.

The initial attempt was not successful as the component was incompatible with multi-homed hosts (those with multiple IP addresses / network interfaces), because it listen to port 80 on the primary network interface only.

As currently the component was running on a single-homed host, there’s no problem, since there’s only one IP address, and no ambiguity.

But VCS clusters are multi-homed devices – they have local IP, and cluster IP.

And in VCS world, nobody is connecting to host name / IP directly, connection should be established to cluster name / IP.

So to make the component publish web service on all interfaces, instead of using localhost or host address, use as address. With you tell it to ‘listen’ to incoming connections to any ip-address (associated with box).