Monday, January 12, 2015

Hashset Array in Java Program



How to copy all elements from HashSet to an array

package com.java2novice.hashset;
import java.util.HashSet;
public class MyHashSetToArray
{
public static void main(String a[])
{
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println("HashSet content: ");
        System.out.println(hs);
        String[] strArr = new String[hs.size()];
        hs.toArray(strArr);
        System.out.println("Copied array content:");
        for(String str:strArr)
        {
            System.out.println(str);
        }
    }
}

How to compare two sets and retain elements which are same on both sets

package com.java2novice.hashset;
import java.util.HashSet;
public class MyHashSetRetain
{
public static void main(String a[])
    {
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        hs.add("apple");
        hs.add("rat");
        System.out.println(hs);
        HashSet subSet = new HashSet();
        subSet.add("rat");
        subSet.add("second");
        subSet.add("first");
        hs.retainAll(subSet);
        System.out.println("HashSet content:");
        System.out.println(hs);
    }
}

How to eliminate duplicate user defined objects from HashSet

package com.java2novice.hashset;
import java.util.HashSet;
public class MyDistElementEx
{
public static void main(String a[])
{
        HashSet lhm = new HashSet();
        lhm.add(new Price("Banana", 20));
        lhm.add(new Price("Apple", 40));
        lhm.add(new Price("Orange", 30));
        for(Price pr:lhm)
       {
            System.out.println(pr);
        }
        Price duplicate = new Price("Banana", 20);
        System.out.println("inserting duplicate object...");
        lhm.add(duplicate);
        System.out.println("After insertion:");
        for(Price pr:lhm)
        {
            System.out.println(pr);
        }
    }
}
class Price
 {
    private String item;
    private int price; 
public Price(String itm, int pr)
    {
        this.item = itm;
        this.price = pr;
    }     
 public int hashCode()
   {
        System.out.println("In hashcode");
        int hashcode = 0;
        hashcode = price*20;
        hashcode += item.hashCode();
        return hashcode;
    }
public boolean equals(Object obj)
 {
        System.out.println("In equals");
        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;
    }
}

How to find user defined objects from HashSet

package com.java2novice.hashset;
import java.util.HashSet;
public class MyHashSetSearchObject
{
 public static void main(String a[])
{
        HashSet lhs = new HashSet();
        lhs.add(new Price("Banana", 20));
        lhs.add(new Price("Apple", 40));
        lhs.add(new Price("Orange", 30));
        for(Price pr:lhs)
        {
            System.out.println(pr);
        }
        Price key = new Price("Banana", 20);
        System.out.println("Does set contains key? "+lhs.contains(key));
    }
}
class Price
    private String item;
    private int price;
  public Price(String itm, int pr)
 {
        this.item = itm;
        this.price = pr;
    }
public int hashCode()
  {
        System.out.println("In hashcode");
        int hashcode = 0;
        hashcode = price*20;
        hashcode += item.hashCode();
        return hashcode;
    }
 public boolean equals(Object obj)
 {
        System.out.println("In equals");
        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