File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about HashSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about HashSet" Watch "Question about HashSet" New topic

Question about HashSet

Robbi Palacios
Ranch Hand

Joined: May 25, 2006
Posts: 96
I was wondering why HashSet and LinkedSet are printing out duplicate
values. like:

No Duplicates [Starwars George Lucas, The Nurse Phoomba, The Apprentice Frodo, The Apprentice Frodo, The Nurse Phoomba, Somersault Kaneevil, The Apprentice Frodo]

But when I use TreeSet the duplicate values are eliminated
as follows:

No Duplicates [Somersault Kaneevil, Starwars George Lucas, The Apprentice Frodo, The Nurse Phoomba]

Can you guys help me figure out why HashSet and LinkedSet are not
able to eliminate these duplicates? Please...

My Code are as follows:

public class TestMovies {

public static void main(String[] args) {
List<Movie> movies = new ArrayList<Movie>();

Movie[] collection = { new Movie("The Apprentice", "Frodo"),
new Movie("Somersault", "Kaneevil"),
new Movie("The Nurse", "Phoomba"),
new Movie("Starwars", "George Lucas"),
new Movie("The Apprentice", "Frodo"),
new Movie("The Apprentice", "Frodo"),
new Movie("The Nurse", "Phoomba") };

movies = Arrays.asList(collection);
System.out.println("Unsorted " + movies);

//Set<Movie> movieSet = new TreeSet<Movie>(); <-- Removes Duplicates
Set<Movie> movieSet = new HashSet<Movie>(); //<-- Doesn't remove duplicates
//Collections.sort(movieSet); Can only sort Lists !
System.out.println("No Duplicates " + movieSet );

This is my class:

public class Movie implements Comparable<Movie> {
private String title;
private String actor;

public Movie(String title, String actor){
this.title = title; = actor;

public String getTitle() {
return title;
public String getActor() {
return actor;

public String toString(){
return title + " " + actor;

public boolean equals(Movie obj) {
if ( obj == this) {
return true;
if(!(obj instanceof Movie)) {
return false;
//return getTitle().equals(obj.getTitle());
return this.title == obj.getTitle() && == obj.getActor();

public int hashCode(){
int hashValue = 12;
hashValue = 31 * hashValue + title.hashCode();
hashValue = 31 * hashValue + actor.hashCode();
return hashValue;
//return title.hashCode();

public int compareTo(Movie movie) {
return title.compareTo(movie.getTitle());


Sun Certified Java Programmer 5.0
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
The problem is that in your Movie class, you are not overriding the equals method from Object, you are overloading it.

Remember the signature of the equals method in Object is equals(Object)
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Because your equals() method isn't being called. When you override a method, you can't change the parameter types -- if you do, it's overloading, not overriding. The argument to equals must be of type Object; after your instanceof test (which here is silly -- of COURSE the argument is a Movie, since that's the type of the parameter!) you'd cast to the correct type.

[Jess in Action][AskingGoodQuestions]
Robbi Palacios
Ranch Hand

Joined: May 25, 2006
Posts: 96
Thank you so much for the help. I thought before maybe if i replaced Frodo
in the Apprentice it'll work. I used so much imagination in this one.
I agree. Here's the link:
subject: Question about HashSet
It's not a secret anymore!