Unit testing: Prevent creation of duplicate objectsHow should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?Unit test naming best practicesHow do you assert that a certain exception is thrown in JUnit 4 tests?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Wrong ordering in generated table in jpaHow to deserialize a list using GSON or another JSON library in Java?
Extract the characters before last colon
What was the ring Varys took off?
Why was my Canon Speedlite 600EX triggering other flashes?
A case where Bishop for knight isn't a good trade
Why are solar panels kept tilted?
Generate ladder of integers using the least number of unique characters (in C++)
Biology of a Firestarter
Who commanded or executed this action in Game of Thrones S8E5?
Will the volt, ampere, ohm or other electrical units change on May 20th, 2019?
Filter a data-frame and add a new column according to the given condition
How to redirect stdout to a file, and stdout+stderr to another one?
Given 0s on Assignments with suspected and dismissed cheating?
Will a coyote attack my dog on a leash while I'm on a hiking trail?
How can we allow remote players to effectively interact with a physical tabletop battle-map?
Getting and editing list of strings from file geodatabase
White foam around tubeless tires
Is this possible when it comes to the relations of P, NP, NP-Hard and NP-Complete?
Why did the soldiers of the North disobey Jon?
Why do galaxies collide
Wireless headphones interfere with Wi-Fi signal on laptop
How to cope with regret and shame about not fully utilizing opportunities during PhD?
How to make a not so good looking person more appealing?
Did galley captains put corks in the mouths of slave rowers to keep them quiet?
Why is it harder to turn a motor/generator with shorted terminals?
Unit testing: Prevent creation of duplicate objects
How should I unit test threaded code?How do I test a private function or a class that has private methods, fields or inner classes?Unit Testing C CodeIs Unit Testing worth the effort?Unit test naming best practicesHow do you assert that a certain exception is thrown in JUnit 4 tests?JavaScript unit test tools for TDDWhat is Unit test, Integration Test, Smoke test, Regression Test?Wrong ordering in generated table in jpaHow to deserialize a list using GSON or another JSON library in Java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
My professor gave us this homework exercise and has created a project with
a bunch of unit tests.
Our goal is to make sure we can pass those unit tests.
We have three classes.
A class called Person with a name and an age,
a class Speaker that extends Person,
and a class Attendee that also extends Person.
I am struggling with making sure that there are no duplicate people. generateRandomString()
was implemented by the professor and just returns a random string.
I already created the class,
it's constructor,
getters,
and setters.
I also overrode the method equals()
in the class Person
This is the test our professor gave us:
@Test
public void testNoDuplicatePerson()
HashSet<Person> people = new HashSet<Person>();
String name = generateRandomString();
Person p = new Speaker(name);
people.add(p);
assertEquals(1,people.size());
p = new Attendee(name);
people.add(p);
assertEquals(1,people.size());
How can I pass this test?
EDIT: I decided to post the code of the three classes:
Person
```java
public abstract class Person
private String name;
private int age;
public Person(String name)
this.name = name;
this.age = 0;
public Person(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public boolean equals(Object o)
if (o == null
Speaker:
public class Speaker extends Person
private int fee;
public Speaker(String name)
super(name);
this.fee = 0;
public Speaker(String name, int age)
super(name, age);
this.fee = 0;
public Speaker(String name, int age, int fee)
super(name, age);
this.fee = fee;
public int getFee()
return fee;
public void setFee(int fee)
this.fee = fee;
public String toString()
return "Speaker " + this.getName() + " as a fee value of " + this.getFee() + ".";
Attendee:
public class Attendee extends Person
private boolean paid;
public Attendee(String name)
super(name);
this.paid=false;
public Attendee(String name, int age)
super(name, age);
this.paid=false;
public boolean hasPaid()
if (this.paid==true)
return true;
return false;
public String toString()
return "Attendee "+this.getName()+(this.hasPaid() ? " has":" hasn't")+" paid its registration.";
java unit-testing
|
show 6 more comments
My professor gave us this homework exercise and has created a project with
a bunch of unit tests.
Our goal is to make sure we can pass those unit tests.
We have three classes.
A class called Person with a name and an age,
a class Speaker that extends Person,
and a class Attendee that also extends Person.
I am struggling with making sure that there are no duplicate people. generateRandomString()
was implemented by the professor and just returns a random string.
I already created the class,
it's constructor,
getters,
and setters.
I also overrode the method equals()
in the class Person
This is the test our professor gave us:
@Test
public void testNoDuplicatePerson()
HashSet<Person> people = new HashSet<Person>();
String name = generateRandomString();
Person p = new Speaker(name);
people.add(p);
assertEquals(1,people.size());
p = new Attendee(name);
people.add(p);
assertEquals(1,people.size());
How can I pass this test?
EDIT: I decided to post the code of the three classes:
Person
```java
public abstract class Person
private String name;
private int age;
public Person(String name)
this.name = name;
this.age = 0;
public Person(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public boolean equals(Object o)
if (o == null
Speaker:
public class Speaker extends Person
private int fee;
public Speaker(String name)
super(name);
this.fee = 0;
public Speaker(String name, int age)
super(name, age);
this.fee = 0;
public Speaker(String name, int age, int fee)
super(name, age);
this.fee = fee;
public int getFee()
return fee;
public void setFee(int fee)
this.fee = fee;
public String toString()
return "Speaker " + this.getName() + " as a fee value of " + this.getFee() + ".";
Attendee:
public class Attendee extends Person
private boolean paid;
public Attendee(String name)
super(name);
this.paid=false;
public Attendee(String name, int age)
super(name, age);
this.paid=false;
public boolean hasPaid()
if (this.paid==true)
return true;
return false;
public String toString()
return "Attendee "+this.getName()+(this.hasPaid() ? " has":" hasn't")+" paid its registration.";
java unit-testing
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create aPerson
object if I already have aPerson
object that has a certain name. The line of codePerson p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this
– Mário Marques
Mar 23 at 14:24
1
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
From the java documentation: "public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.
– Mário Marques
Mar 23 at 14:36
You got it right.
– JB Nizet
Mar 23 at 14:37
|
show 6 more comments
My professor gave us this homework exercise and has created a project with
a bunch of unit tests.
Our goal is to make sure we can pass those unit tests.
We have three classes.
A class called Person with a name and an age,
a class Speaker that extends Person,
and a class Attendee that also extends Person.
I am struggling with making sure that there are no duplicate people. generateRandomString()
was implemented by the professor and just returns a random string.
I already created the class,
it's constructor,
getters,
and setters.
I also overrode the method equals()
in the class Person
This is the test our professor gave us:
@Test
public void testNoDuplicatePerson()
HashSet<Person> people = new HashSet<Person>();
String name = generateRandomString();
Person p = new Speaker(name);
people.add(p);
assertEquals(1,people.size());
p = new Attendee(name);
people.add(p);
assertEquals(1,people.size());
How can I pass this test?
EDIT: I decided to post the code of the three classes:
Person
```java
public abstract class Person
private String name;
private int age;
public Person(String name)
this.name = name;
this.age = 0;
public Person(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public boolean equals(Object o)
if (o == null
Speaker:
public class Speaker extends Person
private int fee;
public Speaker(String name)
super(name);
this.fee = 0;
public Speaker(String name, int age)
super(name, age);
this.fee = 0;
public Speaker(String name, int age, int fee)
super(name, age);
this.fee = fee;
public int getFee()
return fee;
public void setFee(int fee)
this.fee = fee;
public String toString()
return "Speaker " + this.getName() + " as a fee value of " + this.getFee() + ".";
Attendee:
public class Attendee extends Person
private boolean paid;
public Attendee(String name)
super(name);
this.paid=false;
public Attendee(String name, int age)
super(name, age);
this.paid=false;
public boolean hasPaid()
if (this.paid==true)
return true;
return false;
public String toString()
return "Attendee "+this.getName()+(this.hasPaid() ? " has":" hasn't")+" paid its registration.";
java unit-testing
My professor gave us this homework exercise and has created a project with
a bunch of unit tests.
Our goal is to make sure we can pass those unit tests.
We have three classes.
A class called Person with a name and an age,
a class Speaker that extends Person,
and a class Attendee that also extends Person.
I am struggling with making sure that there are no duplicate people. generateRandomString()
was implemented by the professor and just returns a random string.
I already created the class,
it's constructor,
getters,
and setters.
I also overrode the method equals()
in the class Person
This is the test our professor gave us:
@Test
public void testNoDuplicatePerson()
HashSet<Person> people = new HashSet<Person>();
String name = generateRandomString();
Person p = new Speaker(name);
people.add(p);
assertEquals(1,people.size());
p = new Attendee(name);
people.add(p);
assertEquals(1,people.size());
How can I pass this test?
EDIT: I decided to post the code of the three classes:
Person
```java
public abstract class Person
private String name;
private int age;
public Person(String name)
this.name = name;
this.age = 0;
public Person(String name, int age)
this.name = name;
this.age = age;
public String getName()
return name;
public void setName(String name)
this.name = name;
public int getAge()
return age;
public void setAge(int age)
this.age = age;
public boolean equals(Object o)
if (o == null
Speaker:
public class Speaker extends Person
private int fee;
public Speaker(String name)
super(name);
this.fee = 0;
public Speaker(String name, int age)
super(name, age);
this.fee = 0;
public Speaker(String name, int age, int fee)
super(name, age);
this.fee = fee;
public int getFee()
return fee;
public void setFee(int fee)
this.fee = fee;
public String toString()
return "Speaker " + this.getName() + " as a fee value of " + this.getFee() + ".";
Attendee:
public class Attendee extends Person
private boolean paid;
public Attendee(String name)
super(name);
this.paid=false;
public Attendee(String name, int age)
super(name, age);
this.paid=false;
public boolean hasPaid()
if (this.paid==true)
return true;
return false;
public String toString()
return "Attendee "+this.getName()+(this.hasPaid() ? " has":" hasn't")+" paid its registration.";
java unit-testing
java unit-testing
edited Mar 23 at 20:42
DwB
29.6k84474
29.6k84474
asked Mar 23 at 14:14
Mário MarquesMário Marques
95
95
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create aPerson
object if I already have aPerson
object that has a certain name. The line of codePerson p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this
– Mário Marques
Mar 23 at 14:24
1
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
From the java documentation: "public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.
– Mário Marques
Mar 23 at 14:36
You got it right.
– JB Nizet
Mar 23 at 14:37
|
show 6 more comments
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create aPerson
object if I already have aPerson
object that has a certain name. The line of codePerson p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this
– Mário Marques
Mar 23 at 14:24
1
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
From the java documentation: "public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.
– Mário Marques
Mar 23 at 14:36
You got it right.
– JB Nizet
Mar 23 at 14:37
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create a
Person
object if I already have a Person
object that has a certain name. The line of code Person p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this– Mário Marques
Mar 23 at 14:24
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create a
Person
object if I already have a Person
object that has a certain name. The line of code Person p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this– Mário Marques
Mar 23 at 14:24
1
1
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
From the java documentation: "
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.– Mário Marques
Mar 23 at 14:36
From the java documentation: "
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.– Mário Marques
Mar 23 at 14:36
You got it right.
– JB Nizet
Mar 23 at 14:37
You got it right.
– JB Nizet
Mar 23 at 14:37
|
show 6 more comments
1 Answer
1
active
oldest
votes
As mentioned by @JB Nizet, when you override the equals
method of a class,
you must override the hashCode
method.
Note the name of the class in the jUnit test: HashSet.
That depends on the hashCode
method.
Solution:
a. Learn to read the java API docs.
Here is a link to the v8 JavaDocs.
Read the HashSet
page.
b. Implement the hashCode
method.
You clearly have Internet access,
so, if you don't know how to implement a hashCode
method,
try a google search for "how do I implement a Java hashCode method".
Hint: String already implements a hashCode method.
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314608%2funit-testing-prevent-creation-of-duplicate-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As mentioned by @JB Nizet, when you override the equals
method of a class,
you must override the hashCode
method.
Note the name of the class in the jUnit test: HashSet.
That depends on the hashCode
method.
Solution:
a. Learn to read the java API docs.
Here is a link to the v8 JavaDocs.
Read the HashSet
page.
b. Implement the hashCode
method.
You clearly have Internet access,
so, if you don't know how to implement a hashCode
method,
try a google search for "how do I implement a Java hashCode method".
Hint: String already implements a hashCode method.
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
add a comment |
As mentioned by @JB Nizet, when you override the equals
method of a class,
you must override the hashCode
method.
Note the name of the class in the jUnit test: HashSet.
That depends on the hashCode
method.
Solution:
a. Learn to read the java API docs.
Here is a link to the v8 JavaDocs.
Read the HashSet
page.
b. Implement the hashCode
method.
You clearly have Internet access,
so, if you don't know how to implement a hashCode
method,
try a google search for "how do I implement a Java hashCode method".
Hint: String already implements a hashCode method.
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
add a comment |
As mentioned by @JB Nizet, when you override the equals
method of a class,
you must override the hashCode
method.
Note the name of the class in the jUnit test: HashSet.
That depends on the hashCode
method.
Solution:
a. Learn to read the java API docs.
Here is a link to the v8 JavaDocs.
Read the HashSet
page.
b. Implement the hashCode
method.
You clearly have Internet access,
so, if you don't know how to implement a hashCode
method,
try a google search for "how do I implement a Java hashCode method".
Hint: String already implements a hashCode method.
As mentioned by @JB Nizet, when you override the equals
method of a class,
you must override the hashCode
method.
Note the name of the class in the jUnit test: HashSet.
That depends on the hashCode
method.
Solution:
a. Learn to read the java API docs.
Here is a link to the v8 JavaDocs.
Read the HashSet
page.
b. Implement the hashCode
method.
You clearly have Internet access,
so, if you don't know how to implement a hashCode
method,
try a google search for "how do I implement a Java hashCode method".
Hint: String already implements a hashCode method.
answered Mar 23 at 20:49
DwBDwB
29.6k84474
29.6k84474
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
add a comment |
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
Thanks alot for everything. I've managed to fix the issue
– Mário Marques
Mar 23 at 20:50
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314608%2funit-testing-prevent-creation-of-duplicate-objects%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What does the javadoc of HashSet say? When does the set consider that an object is a duplicate of another object? What do you conclude? docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-
– JB Nizet
Mar 23 at 14:18
You are not supposed to change the unit test itself, only your code. From what I have understood is that I shouldn't be able to create a
Person
object if I already have aPerson
object that has a certain name. The line of codePerson p=new Speaker(name);
should not work as I already have a person with the name. My question is how to solve this– Mário Marques
Mar 23 at 14:24
1
No. You're free to have as many persons with the same name as you want. That's not what the test is testing. What the test is testing is that, if you try to add a person to a HashSet with the same name as another person already in the HashSet, then the HashSet should consider them as duplicates (since the size of the set stays at 1 after the addition). Hence my question: when does the set consider that an object is a duplicate of another object?
– JB Nizet
Mar 23 at 14:27
From the java documentation: "
public boolean add(E e)
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false." I obviously have to override the equals method.– Mário Marques
Mar 23 at 14:36
You got it right.
– JB Nizet
Mar 23 at 14:37