JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: How to find user defined objects from HashSet?


Description:

Below example shows how to search user defined objects from HashSet. You can achieve this by implementing equals and hashcode methods at the user defined objects.


Code:
package com.java2novice.hashset;

import java.util.HashSet;

public class MyHashSetSearchObject {

	public static void main(String a[]){
		
		HashSet<Price> lhs = new HashSet<Price>();
		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;
	}
}

Output:
In hashcode
In hashcode
In hashcode
item: Apple  price: 40
item: Orange  price: 30
item: Banana  price: 20
In hashcode
In equals
Does set contains key? true
<< Previous Program | Next Program >>

List Of All HashSet Sample Programs:

  1. Basic HashSet Operations.
  2. How to iterate through HashSet?
  3. How to copy Set content to another HashSet?
  4. How to delete all elements from HashSet?
  5. How to copy all elements from HashSet to an array?
  6. How to compare two sets and retain elements which are same on both sets?
  7. How to eliminate duplicate user defined objects from HashSet?
  8. How to find user defined objects from HashSet?
  9. How to delete user defined objects from HashSet?
Knowledge Centre
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.