Publish Your Article Here

Program: How to eliminate duplicate user defined objects from LinkedHashSet?


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

package com.java2novice.linkedhashset;

import java.util.LinkedHashSet;

public class MyDistElementEx {

	public static void main(String a[]){
		LinkedHashSet<Price> lhm = new LinkedHashSet<Price>();
		lhm.add(new Price("Banana", 20));
		lhm.add(new Price("Apple", 40));
		lhm.add(new Price("Orange", 30));
		for(Price pr:lhm){
		Price duplicate = new Price("Banana", 20);
		System.out.println("inserting duplicate object...");
		System.out.println("After insertion:");
		for(Price pr:lhm){

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;

In hashcode
In hashcode
In hashcode
item: Banana  price: 20
item: Apple  price: 40
item: Orange  price: 30
inserting duplicate object...
In hashcode
In equals
After insertion:
item: Banana  price: 20
item: Apple  price: 40
item: Orange  price: 30
<< Previous Program | Next Program >>

List Of All LinkedHashSet Sample Programs:

  1. Basic LinkedHashSet Operations.
  2. How to delete all elements from LinkedHashSet?
  3. How to add another collection to LinkedHashSet?
  4. How to iterate through LinkedHashSet?
  5. How to compare two LinkedHashSet and retain elements which are same on both LinkedHashSet?
  6. How to copy content of LinkedHashSet to an array?
  7. How to delete specific element from LinkedHashSet?
  8. How to search an object from LinkedHashSet?
  9. How to eliminate duplicate user defined objects from LinkedHashSet?
  10. How to find user defined objects from LinkedHashSet?
  11. How to delete user defined objects from LinkedHashSet?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
Where can we use serialization?
Whenever an object has to sent over the network, those objects should be serialized. Also if the state of an object is to be saved, objects need to be serilazed.
Famous Quotations
There is a great difference between worry and concern. A worried person sees a problem, and a concerned person solves a problem.
-- Harold Stephens

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 © 2019 by Nataraja Gootooru. All Rights Reserved.