How to use a Map in Java (HashMap,LinkedHashMap)


Use the following source code;

Product.java
package javamapexample;

/**
 *
 * @author tunatore
 */
public class Product {

private int id;
private String name;
private int price;

public Product(int id,String name,int price) {
this.id = id;
this.name = name;
this.price = price;
}
/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the price
*/
public int getPrice() {
return price;
}

/**
* @param price the price to set
*/
public void setPrice(int price) {
this.price = price;
}

}

Main.java

package javamapexample;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 *
 * @author tunatore
 */
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

     //Lets create first products by using Product class

        Product p1 = new Product(1,”car”,10000);
        Product p2 = new Product(2,”computer”,1000);
        Product p3 = new Product(3,”phone”,400);
        Product p4 = new Product(4,”speakers”,100);
        Product p5 = new Product(5,”speakers2″,125);

        Map<String,Product> productMap = new HashMap<String, Product>();
        productMap.put(“car”, p1);
        productMap.put(“computer”, p2);
        productMap.put(“phone”, p3);
        productMap.put(“speakers”, p4);

        //what will be happened if a same key is inserted again
        //this will replace the old product with a new one
productMap.put(“speakers”, p4);

       //HashMap allows programmers to have only one Null key and many null values

      productMap.put(null, p5);

     //productMap.put(null, null); VALID
    //productMap.put(“phone”, null); VALID

System.out.println(“getting the phone element (id for the phone) with key phone –> ” + productMap.get(“phone”).getId() );

        // Iteration starts
        //ordering of the elements in a HashMap is not sorted
        //so that ordering will be unpredictable
        //use TreeMap if you want ordering
Iterator it = productMap.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
System.out.println(“key –> ” + key + ” value –> ” + ” id -> ” + productMap.get(key).getId()  + ” name -> ” + productMap.get(key).getName());
}

System.out.println(“\nLinkedHashMap starts\n”);

        Map<String,Product> productMapLinked = new LinkedHashMap<String, Product>();
        productMapLinked.put(“1”, p2);
        productMapLinked.put(“2”, p3);
        productMapLinked.put(“3”, p1);

Iterator it2 = productMapLinked.keySet().iterator();
while (it2.hasNext()) {
Object key = it2.next();
System.out.println(“key –> ” + key + ” value –> ” + productMapLinked.get(key).getName());
}

     //If you use TreeMap in your application
        //You  will need to override equals() and hashCode()

}

}

Sample output;

run:
getting the phone element (id for the phone) with key phone –> 3

HashMap starts

key –> null value –>  id -> 5 name -> speakers2
key –> computer value –>  id -> 2 name -> computer
key –> car value –>  id -> 1 name -> car
key –> phone value –>  id -> 3 name -> phone
key –> speakers value –>  id -> 4 name -> speakers

LinkedHashMap starts

key –> 1 value –> computer
key –> 2 value –> phone
key –> 3 value –> car
BUILD SUCCESSFUL (total time: 1 seconds)
Download Source

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