LinkedHashMap vs HashMap

public class MyLinkedHashMap {
    
    public static void main(String[] args) {
        createMap(new LinkedHashMap<String, String>());
        System.out.println("*****");
        createMap(new HashMap<String, String>());
    }

    private static void createMap(Map<String, String> map) {
        map.put("1A", "1A_LOC");
        map.put("EY", "1E_LOC");
        System.out.println(map.values());
        System.out.println(map.entrySet());
        System.out.println(map.keySet());
    }

}

output:
[1A_LOC, 1E_LOC]
[1A=1A_LOC, EY=1E_LOC]
[1A, EY]
*****
[1E_LOC, 1A_LOC]
[EY=1E_LOC, 1A=1A_LOC]
[EY, 1A]

Note: HashMap did not preserve insertion order as 1A was inserted before EY. LinkedHashMap keep the order.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s