This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about HashSet" Watch "Question about HashSet" New topic
Author

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
movieSet.addAll(movies);
//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;
this.actor = 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() && this.actor == 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: 2367
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: http://aspose.com/file-tools
 
subject: Question about HashSet