Monday, January 12, 2015

Java Using Objects from Linked HashSet Program



How to delete specific element from Linked HashSet

package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLhsDeleteEx
{
public static void main(String a[])
{
         
        LinkedHashSet lhs = new LinkedHashSet();
        lhs.add("first");
        lhs.add("second");
        lhs.add("third");
        System.out.println(lhs);
        lhs.remove("second");
        System.out.println("Elements after deleting an element:");
        System.out.println(lhs);
    }
}

How to search an object from Linked HashSet

package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLhsSearchEx
{
public static void main(String a[])
{
        LinkedHashSet lhs = new LinkedHashSet();
        lhs.add("first");
        lhs.add("second");
        lhs.add("third");
        System.out.println(lhs);
        System.out.println("Does set contains 'first'? "+lhs.contains("first"));
    }
}

How to eliminate duplicate user defined objects from Linked HashSet

package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyDistElementEx
{
public static void main(String a[])
{
         LinkedHashSet lhm = new LinkedHashSet();
        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 Linked HashSet

package com.java2novice.linkedhashset;
import java.util.LinkedHashSet;
public class MyLhsUdObjSearch
{
public static void main(String a[])
{
        LinkedHashSet lhs = new LinkedHashSet();
        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