Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

removing dups using Set

Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem removing duplicate objects using LinkedHashSet. I created a test object called MyDimension. I have overridden the equals method and the hashcode method. I think I have overridden them correctly but I may have made a mistake. I get the correct behavior when I call the equals method of MyDimension when comparing two objects, so I don't think there is anything wrong there. I added print statements to the hashcode method and it shows the same hashcode value for the two objects that contain the same data. It there something I am missing?

I just can't figure out why the ArrayList passed to LinkedHashSet still have duplicates. I have included the and

public class MyDimension {
double length;
double width;

public MyDimension(double length, double width) {
this.length = length;
this.width = width;

public double getLength() {
return length;
public double getWidth() {
return width;

public String toString() {
return new String("Length = " + length + " Width = "+ width);

public boolean equals(MyDimension obj) {
if (obj != null && (obj.getClass().equals(this.getClass()))) {
return (width == obj.getWidth()) && (length == obj.getLength());

return false;

public int hashCode() {
long tempValue = (Double.doubleToLongBits(this.length) + Double.doubleToLongBits(this.width));
System.out.println("HASHCODE CALLED VALUE IS " + tempValue);
int tempInt = (int)(tempValue^(tempValue >>> 32));
System.out.println("HASHCODE CALLED VALUE IS " + tempInt);
return tempInt;

import java.util.List;
import java.util.HashSet;
import java.util.ArrayList;
import java.util.LinkedHashSet;

public class Test4 {

public static void main(String args[]) {
MyDimension a = new MyDimension(21.1, 20.1);
MyDimension b = new MyDimension(11.5, 10.5);
MyDimension c = new MyDimension(21.1, 20.1);
MyDimension d = new MyDimension(5.4, 3.4);

ArrayList<MyDimension> myList = new ArrayList<MyDimension>();

LinkedHashSet<MyDimension> set = new LinkedHashSet<MyDimension>();


if (a.equals(b)) {
System.out.println("A equals B");
else {
System.out.println("A does not equal B");
if (a.equals(c)) {
System.out.println("A equals C");
else {
System.out.println("A does not equal C");

for (int i = 0; i < myList.size(); i++) {
System.out.println( myList.get(i).toString() );

author and iconoclast
Posts: 24203
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Welcome to JavaRanch!

You've committed an error that everyone makes once; most people don't make it again.

The equals() method you've written does not override the one in Object, so HashSet, etc, don't use it. Your equals() takes a MyDimension as an argument; Object's takes an Object. Change your equals to take an Object as an argument, check whether the argument is a MyDimension with instanceof, cast it to MyDimension if appropriate, and then do the checks you've shown here. The program will then magically work!
Rich Wright
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I made the recommended changes and everything magically worked just as you said it would .
mooooooo ..... tiny ad ....
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic