Custom objects not found in HashMap? - java

Hi i'm trying to learn the purpose of hashcode () and equals() method.I tried the following program.
import java.util.HashMap;
public class LearnHascode {
public String name;
public int age;
LearnHascode(String na)
{
name = na;
}
public int hashCode()
{
return name.hashCode();
}
public boolean equals(LearnHascode obj)
{
return this.name.equals(obj.name);
}
public static void main(String[] args)
{
HashMap h = new HashMap();
LearnHascode ob1 = new LearnHascode("Prabha");
LearnHascode ob2 = new LearnHascode("Prabha");
h.put(ob1, v1);
h.put(ob2, v2);
System.out.println(h);
System.(h.out.printlncontainsKey(new LearnHascode("Prabha")));
}
}
output :
{hash.LearnHascode#8ef7bdfc=Two, hash.LearnHascode#8ef7bdfc=one}
false
I have two doubts :
1) I thought HashMap will contain one entry as hascode of the two objects (ob1 and ob2) are same. could any one explain why there are two entries in the HashMap?
2) why System.(h.out.printlncontainsKey(new LearnHascode("Prabha"))); returns false?

Your equals() method is wrong, and this breaks the HashMap. The argument to equals() is always an Object; you have to check what kind of Object it is and cast it in the body of the method.
The hashCode() value is used to sort the objects into categories, but equals() is used to decide whether two objects are actually the same. You need to define both of these methods correctly to get HashMap to work.

you did not give proper implementaiton of hashCode() and equals() methods.
public class Employee {
private int empId;
private String empName;
public Employee(int id, String name){
empId = id;
empName = name;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int hashCode(){
System.out.println("In Hash Code");
int hashCode = 20;
hashCode *= this.empId;
hashCode += this.empName.hashCode();
return hashCode;
}
public boolean equals(Object obj){
System.out.println("In equals");
if(!(obj instanceof Employee)){
return false;
}
Employee emp = (Employee) obj;
return (emp.getEmpName().equals(this.getEmpName())) && (emp.getEmpId() == this.getEmpId());
}
}

Related

Unit testing: Prevent creation of duplicate objects

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 || !(o instanceof Person))
return false;
Person converted = (Person) o;
if (this.getName().equals(converted.getName()) && this.getAge() == converted.getAge())
return true;
return false;
}
}
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.";
}
}
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.

Overridden equals and hashCode does not work on custom Object;

I have an below object
class CustomObj{
private String name;
private String dept;
public String getName(){
return this.name;
}
public String getDept(){
return this.dept;
}
private CustomObj(){
}
private CustomObj(CustomObjBuilder builder){
this.name = builder.name;
this.dept= builder.dept;
}
#Override
public boolean equals(Object o) {
if (o == this) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomObj that = (CustomObj) o;
return that.name.equals(name) &&
that.dept.equals(dept);
}
#Override
public int hashCode() {
int result = 31;
result = 31 * result + name.hashCode();
result = 31 * result + dept.hashCode();
return result;
}
public static class CustomObjBuilder{
private String name;
private String dept;
public CustomObjBuilder(String name, String dept){
this.name = name;
this.dept = dept;
}
public CustomObjBuilder setName(String name){
this.name = name;
return this;
}
public CustomObjBuilder setDept(String dept){
this.dept = dept;
return this;
}
public CustomObj build(){
return new CustomObj(this);
}
}
}
and class that uses above
class XYZ{
Set<CustomObj> obj = new HashSet<CustomObj>();
public void process(String a, String b){
CustomObj o = new CustomObj.CustomObjBuilder(a,b).build();
if(!obj.contains(o)){
obj.add(o);
}
}
}
And a test class
class TestXYX{
#Test
public void test(){
XYZ xyz = new XYZ();
xyz.process("TEST","TESTABC");
xyz.process("TEST","TESTABC");
}
}
Beacuse I have overrideen hascode and equals, both the above are equal and when process is called second time, the control should not go into if(!obj.contains(o)) second time and size of the set should be 1. But when i run the test obj.add(o); is called two times. But the values of both this object and that objec inside equals methods are same, but
that.name.equals(name) && that.dept.equals(dept)
inside CustomObj returns false. Can someone please help me understand why?
The code is fine. To verify add an sysout to check Set size:
class XYZ {
Set<CustomObj> obj = new HashSet<CustomObj>();
public void process(String a, String b) {
CustomObj o = new CustomObj.CustomObjBuilder(a, b).build();
if (!obj.contains(o)) { // Fails second time for your use case.
obj.add(o);
}
System.out.println(obj.size()); // This is 1 in your use case.
}
}

Java HashMap: How to replace a own Key Object with put()-Method [duplicate]

This question already has answers here:
Setting own class as key in java Hashmap
(7 answers)
Closed 6 years ago.
lets say I have a Employee-class with Instant- and Id-Attribute:
public class Employee implements Comparable<Employee> {
private Instant workEnd;
private Integer id;
private String name;
public Instant getWorkEnd() {
return workEnd;
}
public void setWorkEnd(Instant workEnd) {
this.workEnd = workEnd;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public int compareTo(Employee employee) {
int workEndCompare = getWorkEnd().compareTo(employee.getWorkEnd());
int idCompare = getId().compareTo(employee.getId());
if (workEndCompare == 0) {
return idCompare;
} else {
return workEndCompare;
}
}
#Override
public String toString() {
return String.format("{Date: %s,Number: %d}", getWorkEnd(), getId());
}
}
As you can see each Employee-Object sorts dependent on workEnd and id.
Now I want to put these Employee-Objects as Keys in a HashMap. But I want that the HashMap replaces each Key-Employee with the put()-method if the attributes workend and id are equal. Thta means I want the normal behaviour of a HashMap but with own custom Objects as Mapkeys.
How I manage that? By implementing the equals-method?
I tried something like that, but it does not work:
#Override
public boolean equals(Object obj) {
if (obj instanceof Employee) {
Employee employee = (Employee) obj;
int workEndCompare = getWorkEnd().compareTo(employee.getWorkEnd());
int idCompare = getId().compareTo(employee.getId());
if ((idCompare + workEndCompare) == 0) {
return true;
} else {
return false;
}
} else {
return super.equals(obj);
}
}
When you implement the equals method you also need to implement the hashcode method too.
There is something called the hashcode - equals contract, both need to be implemented for your hashmap to work,
The answer here explains it well.

Removing Duplicate Objects that contain different variables

I have two objects that have the same name but contain different ages(values), I tried adding these objects to a map to remove duplicates but it won't remove. This is the model code I am testing:
two ab = new two("john", "20");
two ac = new two("chan", "30");
two ad = new two("john", "34");
ArrayList<two> ae = new ArrayList<>();
public void adding(){
ae.add(ab);
ae.add(ac);
ae.add(ad);
System.out.println(ae);
}
public void removeDuplicate(){
Set<two> lhs = new HashSet<>();
lhs.addAll(ae);
ae.clear();
ae.addAll(lhs);
System.out.println(ae);
}
public static void main(String args[]){
one five = new one();
five.adding();
five.removeDuplicate();
}
This is the class that is used for object type:
package teeestserrr;
public class two {
private String name;
private String age;
public two(String name, String age){
this.age = age;
this.name = name;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String toString(){
return name + " " + age;
}
}
Results are :
[john, chan, john]
[chan, john, john]
I also tried to make the toString return only name but the map used to remove duplicates doesn't seem to work even in that case. I don't understand and I cannot identify the underlying problem. Any help is appreciated.
You have to override equals and hashcode method in two.
Equals should give result according to only name field and also hashcode should be formed using only name field, that will help you to achieve your aim.
package teeestserrr;
public class two {
private String name;
private String age;
public two(String name, String age){
this.age = age;
this.name = name;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String toString(){
return name + " " + age;
}
#Override
public boolean equals(Object obj){
if(obj==null)
return false;
if(name==null){
return false;
}
if(!(obj instanceof two)){
return false;
}
two another = (two)obj;
return this.name.equals(another.name);
}
public int hashCode(){
return name==null?0:name.hashCode();
}
}

How to use Java comparator properly?

If I have the following class:
public class Employee {
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
// set values on attributes
}
// getters & setters
}
How can I use comparator that compares by name, then age, then id?
You need to implement it so that it orders by preferred elements. That is, you need to compare by name, then if that comparison is equal, compare by age, etc. An example is listed below:
public class EmployeeComparator implements Comparator<Employee> {
#Override
public int compare(Employee e1, Employee e2) {
int nameDiff = e1.getName().compareTo(e2.getName());
if(nameDiff != 0) {
return nameDiff;
}
int ageDiff = e1.getAge() - e2.getAge();
if(ageDiff != 0) {
return ageDiff;
}
int idDiff = e1.getEmpId() - e2.getEmpId();
return idDiff;
}
}
Update
Came across this a moment ago: How to compare objects by multiple fields One of the answers linked to ComparatorChain which will invoke multiple comparators in sequence until a non-zero result is received from a comparator or all comparators are invoked. This should probably be your preferred solution.
Perhaps this (untested) implementation of Comparator#compare() will do the trick.
int compare(Employee e, Employee f)
{
int val = e.name.compareTo(f.name);
if(val == 0)
{
val = e.age - f.age;
if(val == 0)
{
val = e.empId - f.empId;
}
}
return val;
}
You can also implement the Comparable Interface in your class.
for example, something like this:
public class Employee implements Comparable<Employee>{
private int empId;
private String name;
private int age;
public Employee(int empId, String name, int age) {
// set values on attributes
}
// getters & setters
public int compareTo(Employee o) {
int ret = this.name.compareTo(o.name);
if(ret == 0)
ret = this.age - o.age;
if(ret == 0)
ret = this.empId - o.empId;
return ret;
}
}
so you don't have to implement a extra class to compare your Employees.
Implement it
public class Employee {
private int empId;
private String name;
private int age;
/**
* #param empId
* #param name
* #param age
*/
public Employee(int empId, String name, int age) {
super();
this.empId = empId;
this.name = name;
this.age = age;
}
/**
*
*/
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
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;
}
//Compare by name, age and then id
public static Comparator<Employee> COMPARE_EMPLOYEE = new Comparator<Employee>() {
public int compare(Employee one, Employee other) {
//Compare Name
if (one.getName().compareToIgnoreCase(other.getName()) == 0) {
//Compare age
if((one.getAge() - other.getAge()) == 0) {
// Now check with id is useless
// So directly return result of compare by id
return one.getEmpId() - other.getEmpId();
} else { //If age Not equal
return one.getAge() - other.getAge();
}
} else { //If name not equal
return one.getName().compareToIgnoreCase(other.getName());
}
}
};
}
Use :
List<Employee> contacts = new ArrayList<Employee>();
//Fill it.
//Sort by address.
Collections.sort(contacts, Employee.COMPARE_EMPLOYEE);
Read Sorting an ArrayList of Contacts , this must help you and you will get more ideas and different different types of use of Comparator.
guava ComparisonChain:
List<Employee> list = new ArrayList<Employee>();
//...
Collections.sort(list, new Comparator<Employee>(){
#Override
public int compare(Employee e1, Employee e2) {
return ComparisonChain.start()
.compare(e1.empId, e2.empId)
.compare(e1.name, e2.name)
.compare(e1.age, e2.age).result();
}});
Use this:
public class Test
{
public static void main(String[] args)
{
Employee emp1 = new Employee(2, "Tom", 20);
Employee emp2 = new Employee(1, "Tom", 20);
Employee emp3 = new Employee(3, "Hank", 21);
List<Employee> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
Collections.sort(list, new Employee().new MyComparator());
System.out.println(list);
}
}
class Employee
{
private int empId;
private String name;
private int age;
public Employee()
{}
public Employee(int empId, String name, int age)
{
this.empId = empId;
this.name = name;
this.age = age;
}
class MyComparator implements Comparator<Employee>
{
#Override
public int compare(Employee e1, Employee e2)
{
if(e1.name.compareTo(e2.name) == 0)
{
if(((Integer)e1.age).compareTo(e2.age) == 0)
{
return ((Integer)e1.empId).compareTo(e2.empId);
}
else
{
return ((Integer)e1.age).compareTo(e2.age);
}
}
return e1.name.compareTo(e2.name);
}
}
#Override
public String toString()
{
return "Employee [empId=" + empId + ", name=" + name + ", age=" + age + "]";
}
}
The Comparator interface defines two methods: compare() and equals().
The compare() method, compares two elements for order:
int compare(Object obj1, Object obj2)
obj1 and obj2 are the objects to be compared. This method returns zero if the objects are equal. It returns a positive value if obj1 is greater than obj2. Otherwise, a negative value is returned.
By overriding compare(), you can alter the way that objects are ordered. For example, to sort in a reverse order, you can create a comparator that reverses the outcome of a comparison.
The equals() method, tests whether an object equals the invoking comparator: boolean equals(Object obj)
obj is the object to be tested for equality. The method returns true if obj and the invoking object are both Comparator objects and use the same ordering. Otherwise, it returns false.
Example:
import java.util.*;
class Dog implements Comparator<Dog>, Comparable<Dog> {
private String name;
private int age;
Dog() {
}
Dog(String n, int a) {
name = n;
age = a;
}
public String getDogName() {
return name;
}
public int getDogAge() {
return age;
}
// Overriding the compareTo method
public int compareTo(Dog d) {
return (this.name).compareTo(d.name);
}
// Overriding the compare method to sort the age
public int compare(Dog d, Dog d1) {
return d.age - d1.age;
}
}
public class Example {
public static void main(String args[]) {
// Takes a list o Dog objects
List<Dog> list = new ArrayList<Dog>();
list.add(new Dog("Shaggy", 3));
list.add(new Dog("Lacy", 2));
list.add(new Dog("Roger", 10));
list.add(new Dog("Tommy", 4));
list.add(new Dog("Tammy", 1));
Collections.sort(list); // Sorts the array list
for(Dog a: list) // printing the sorted list of names
System.out.print(a.getDogName() + ", ");
// Sorts the array list using comparator
Collections.sort(list, new Dog());
System.out.println(" ");
for(Dog a: list) // printing the sorted list of ages
System.out.print(a.getDogName() +" : "+ a.getDogAge() + ", ");
}
}
Check it out for more Java Comparator examples.

Categories

Resources