File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes removing dups using Set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "removing dups using Set" Watch "removing dups using Set" New topic

removing dups using Set

Rich Wright

Joined: Feb 19, 2008
Posts: 2
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() );

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189


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!

[Jess in Action][AskingGoodQuestions]
Rich Wright

Joined: Feb 19, 2008
Posts: 2
Thanks for the help. I made the recommended changes and everything magically worked just as you said it would .
I agree. Here's the link:
subject: removing dups using Set
It's not a secret anymore!