Monday, January 12, 2015

Java Using Hashset Program



Basic Hash Set Operations

package com.java2novice.hashset;
import java.util.HashSet;
public class MyBasicHashSet
{
public static void main(String a[])
{
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println(hs);
        System.out.println("Is HashSet empty? "+hs.isEmpty());
        hs.remove("third");
        System.out.println(hs);
        System.out.println("Size of the HashSet: "+hs.size());
        System.out.println("Does HashSet contains first element? "+hs.contains("first"));
    }
}

How to iterate through Hash Set

package com.java2novice.hashset;
import java.util.HashSet;
import java.util.Iterator;
public class MyHashSetRead
{
public static void main(String a[])
{
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        Iterator itr = hs.iterator();
        while(itr.hasNext())
       {
        System.out.println(itr.next());
        }
    }
}

How to copy Set content to another HashSet

package com.java2novice.hashset;
import java.util.HashSet;
public class MyHashSetCopy
{
public static void main(String a[])
{
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println(hs);
        HashSet subSet = new HashSet();
        subSet.add("s1");
        subSet.add("s2");
        hs.addAll(subSet);
        System.out.println("HashSet content after adding another collection:");
        System.out.println(hs);
    }
}

How to delete all elements from HashSet

package com.java2novice.hashset;
import java.util.HashSet;
public class MyHashSetClear
{
 public static void main(String a[])
{
        HashSet hs = new HashSet();
        hs.add("first");
        hs.add("second");
        hs.add("third");
        System.out.println("My HashSet content:");
        System.out.println(hs);
        System.out.println("Clearing HashSet:");
        hs.clear();
        System.out.println("Content After clear:");
        System.out.println(hs);
    }
}

How to delete user defined objects from HashSet

package com.java2novice.hashset;
import java.util.HashSet;
public class MylhsDeleteObject
{
 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("deleting key from set...");
        lhs.remove(key);
        System.out.println("Elements after delete:");
        for(Price pr:lhs)
        {
            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;
    }
}

No comments:

Post a Comment