Searching...
Saturday, 6 October 2012

Java Map: LinkedHashMap Example

07:11
·         Hash table and linked list implementation of the Map interface
·         LinkedHashMap differs from HashMap in that the order is maintained
·         Performance is below that of HashMap, due to the expense of maintaining the linked list
·         Its methods are not synchronized
 

Example

package com.vinod.collections;

import java.util.LinkedHashMap;
import java.util.Map;

public class LinkedHashMapExample {

    public static void main(String[] args) {
        Map<Object, String> lHashm = new LinkedHashMap<Object, String>();
        lHashm.put(1, "Bangalore");
        lHashm.put(2, "Chennai");
        lHashm.put(3, "Cochin");
        lHashm.put(4, "Hydrabad");
        System.out.println("The Elements are : " + lHashm);
        for (Map.Entry<Object, String> entry : lHashm.entrySet()) {
            System.out.println(entry.getKey() + "   :" + entry.getValue());
        }

    }

}
 

Output

The Elements are : {1=Bangalore, 2=Chennai, 3=Cochin, 4=Hydrabad}

1   :Bangalore

2   :Chennai

3   :Cochin

4   :Hydrabad

 

 

0 comments: