Monday, January 12, 2015

Java Excellant Program



How to get all keys from HashMap

package com.java2novice.hashmap;
import java.util.HashMap;
import java.util.Set;
public class MyHashMapKeys
{
public static void main(String a[])
{
        HashMap hm = new HashMap();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println(hm);
        Set keys = hm.keySet();
        for(String key: keys)
        {
            System.out.println(key);
        }
    }
}

How to get entry set from HashMap

package com.java2novice.hashmap;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class MyHashMapEntrySet
{
public static void main(String a[])
{
        HashMap hm = new HashMap();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println(hm);
        Set> entires = hm.entrySet();
        for(Entry ent:entires)
        {
            System.out.println(ent.getKey()+" ==> "+ent.getValue());
        }
    }
}

How to delete all elements from HashMap

package com.java2novice.hashmap;
import java.util.HashMap;
public class MyHashMapClear
{
 public static void main(String a[])
{
        HashMap hm = new HashMap();
        hm.put("first", "First inserted");
        hm.put("second", "Second inserted");
        hm.put("third","Third inserted");
        System.out.println("My HashMap content:");
        System.out.println(hm);
        System.out.println("Clearing HashMap:");
        hm.clear();
        System.out.println("Content After clear:");
        System.out.println(hm);
    }
}

How to eliminate duplicate user defined objects as a key from HashMap

package com.java2novice.hashmap;
import java.util.HashMap;
import java.util.Set;
public class MyDuplicateKeyEx
{
public static void main(String a[])
{
        HashMap hm = new HashMap();
        hm.put(new Price("Banana", 20), "Banana");
        hm.put(new Price("Apple", 40), "Apple");
        hm.put(new Price("Orange", 30), "Orange");
        printMap(hm);
        Price key = new Price("Banana", 20);
        System.out.println("Adding duplicate key...");
        hm.put(key, "Grape");
        System.out.println("After adding dulicate key:");
        printMap(hm);
    }
    public static void printMap(HashMap map)
    {
        Set keys = map.keySet();
        for(Price p:keys)
        {
            System.out.println(p+"==>"+map.get(p));
        }
    }
}
class Price
{
    private String item;
    private int price;
public Price(String itm, int pr)
{
        this.item = itm;
        this.price = pr;
 }
public int hashCode()
 {
        int hashcode = 0;
        hashcode = price*20;
        hashcode += item.hashCode();
        return hashcode;
    }
  public boolean equals(Object obj)
 {
        if (obj instanceof Price)
       {
            Price pp = (Price) obj;
            return (pp.item.equals(this.item) && pp.price == this.price);
        }
        else
        {
            return false;
        }
    }
  public String getItem()
 {
        return item;
  }
 public void setItem(String item)
    {
        this.item = item;
    }
    public int getPrice()
    {
        return price;
    }
    public void setPrice(int price)
   {
        this.price = price;
    }
    public String toString()
    {
        return "item: "+item+"  price: "+price;
    }
}

No comments:

Post a Comment