Comparing two objects names in an equals method - java

I'm a little lost on how to correctly compare objects that I'm testing. My issue is that the tests themselves always come out as true due to the code, but any other way I think of doesn't work correctly either.
public class Element {
private String atomLetter;
private String name;
public Element(String atomLetter, String name) {
this.atomLetter = atomLetter.toUpperCase();
this.name = name.toLowerCase();
}
public Element(String atomLetter) {
this(atomLetter, "");
}
public String getAtomLetter() {
return atomLetter;
}
public String getName() {
return name;
}
// TODO: two elements are considered to be equal if they have the same atom letter.
#Override
public boolean equals(final Object obj) {
if (atomLetter == this.atomLetter){
return true;
}
return false;
}
#Override
public String toString() {
return "Element{" +
"'" + atomLetter + "'" +
", name='" + name + '\'' +
'}';
}
}
In this case, the outcome comes out exactly the same, but the issue is the equals method.
#Test
public void testSimpleMolecules() {
// simple carbon
Molecule m1 = new Molecule("");
assertTrue(m1.isEmpty());
assertEquals(0, m1.size());
m1.add(new Element("C"));
assertFalse(m1.isEmpty());
assertEquals(1, m1.size());
assertEquals(new Element("C"), m1.get(0));
// simple hydrogen
Molecule m2 = new Molecule("");
m2.add(new Element("H"));
assertFalse(m2.isEmpty());
assertEquals(1, m2.size());
assertEquals(new Element("H"), m2.get(0));
// simple nitrogen
Molecule m3 = new Molecule("");
m3.add(new Element("N"));
assertFalse(m3.isEmpty());
assertEquals(1, m3.size());
assertEquals(new Element("N"), m3.get(0));
// simple oxygen
Molecule m4 = new Molecule("");
m4.add(new Element("O"));
assertFalse(m4.isEmpty());
assertEquals(1, m4.size());
assertEquals(new Element("O"), m4.get(0));
}

In your equals method, you are comparing this object's atomLetter to itself.
if (atomLetter == this.atomLetter){
Instead, you need to cast the obj argument to the Element class and compare its atomLetter to this.atomLetter
Element other = (Element) obj;
return this.atomLettet == other.atomLettet;
Of course, you'll likely want to test that the cast is possible before actually doing it, and say that the objects are not equal if they are of different classes. Also test for null. The javadoc for object.Equals() explains all if the requirements for a proper equals method.

The Answer by DarkSigma is correct about your comparison of this.atomLetter to itself. You have a few other issues with your equals.
Compare content, not references
Your code … == this.atomLetter is comparing object references (pointers) rather than the textual content of those String objects. In other words, you are asking if the two variables both refer to the same object, that is, the same chunk of memory.
Always compare String content by calling String::equals or String::equalsIgnoreCase.
For implementing equals, you can test for references being the same, as a quick first part of the equality testing. But this alone is not enough.
if ( this == other ) return true;
Test for null
You should test for null. If the other object reference is null, there is no object, so there can be no equality.
if ( other == null ) return false;
Test for class
You can also make sure the class of the two objects match.
if ( other == null || getClass() != other.getClass() ) return false;
Cast
As the other Answer mentioned, you should cast the passed Object, having gotten past the class-matching test shown above.
Element element = ( Element ) other;
Check for matching content
As the last test, check for matching content.
In this particular case, I suspect you do care about case matching. So we call String::equals rather than String::equalsIgnoreCase.
return getAtomLetter().equals( element.getAtomLetter() );
Example equals method
Let's pull that all together into a single equals implementation.
#Override
public boolean equals ( Object other )
{
if ( this == other ) return true;
if ( other == null || getClass() != other.getClass() ) return false;
Element element = ( Element ) other;
return getAtomLetter().equals( element.getAtomLetter() );
}
Tip: Your IDE will generate this code for you. No need to write this yourself. For example, in IntelliJ, choose: Code > Generate > equals() and hashCode.
Always implement hashCode when implementing equals
As discussed many times on Stack Overflow, such as here, when writing an equals method, always write a hashCode method using the same logic.
#Override
public int hashCode ( )
{
return Objects.hash( getAtomLetter() );
}
Example class
So we end up with a Element class that looks like this.
package work.basil.example;
import java.util.Objects;
public class Element
{
// Member fields
private String atomLetter, name;
// Constructor
public Element ( String atomLetter , String name )
{
this.atomLetter = Objects.requireNonNull( atomLetter ).toUpperCase();
if ( this.atomLetter.isBlank() ) { throw new IllegalArgumentException();}
this.name = Objects.requireNonNull( name ).toLowerCase();
}
// Getters (read-only).
public String getAtomLetter ( ) {return atomLetter;}
public String getName ( ) {return name;}
// `Object` overrides
#Override
public boolean equals ( Object other )
{
if ( this == other ) return true;
if ( other == null || getClass() != other.getClass() ) return false;
Element element = ( Element ) other;
return getAtomLetter().equals( element.getAtomLetter() );
}
#Override
public int hashCode ( )
{
return Objects.hash( getAtomLetter() );
}
#Override
public String toString ( )
{
return "Element{ " +
"atomLetter='" + atomLetter + '\'' +
" | name='" + name + '\'' +
" }";
}
}

You can implement the equals method this way. However, you also have to implement hashCode for correctness.
For equals
#Override
public boolean equals(Object obj)
{
// check the instance of obj
if (!(obj instanceof Element)) return false;
// check if obj is itself
if (obj == this) return true;
// cast obj as Element
Element e = (Element) obj;
// compare fields
return this.atomLetter.equals(e.atomLetter) &&
this.name.equals(e.name);
}
For hashcode, you can implement it a number of ways, but usually this is the quickest and easiest way.
#Override
public int hashCode()
{
return Objects.hash(atomLetter, name);
}

Related

indexOf() in Android when using Helper

I know that the below code gives the index of that particular element in java.
List<String> list = new ArrayList<>();
list .add("100");
Log.d("TAG",String.valueOf(list.indexOf("300")));
But how to get the index of an element while using a helper Class?
List<HelperClass> Arraylist= new ArrayList<>();
Arraylist.add(new HelperClass(name, email, phoneno));
Log.d("TAG", String.valueOf(new HelperClass(Arraylist.indexOf(name,email,phoneno))));
I searched everywhere for this but couldn't find. Can someone tell me how to find index of a particular item in arraylist while using modal to add data?
Obviously what I have tried is wrong and it shows red line under the whole line but I just typed that code for your understanding of what I want to achieve. Can someone give me a way please?
Helper
#Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + (Email != null ? Emaail.hashCode() : 0);
result = 31 * result + (PhoneNo!= null ? PhoneNo.hashCode() : 0);
return result;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Helper)) return false;
Helperthat = (Helper) o;
if (getName() != null ? !getName().equals(that.getName()) : that.getName() != null)
return false;
if (Email != null ? !Email.equals(that.Email) : that.Email != null)
return false;
if (PhoneNo != null ? !PhoneNo.equals(that.PhoneNo) : that.PhoneNo != null)
return false;
}
ArrayList#indexOf uses the Object#equals comparison method.
If you want to be able to lookup a HelperClass instance inside a Collection, you need to provide your own, overridden, equals method, and possibly also the hashCode one, for use with other, specific, Collection implementations (Map, Set, etc.).
class HelperClass {
...
#Override
public boolean equals(final Object object) {
if (object == this) {
return true;
}
if (!(object instance of HelperClass)) {
return false;
}
final HelperClass other = (HelperClass) object;
return name.equals(other.name) &&
email.equals(other.email) &&
phone.equals(other.phone);
}
}
You obviously need to have an appropriate HelperClass instance to find a match.
final String name = "Name";
final String email = "Email";
final String phone = "Phone";
final HelperClass first = new HelperClass(name, email, phone);
final HelperClass second = new HelperClass(name, email, phone);
final List<HelperClass> helpers = new ArrayList<>(8);
helpers.add(first);
final int index = helpers.indexOf(second); // index = 0
indexOf requires the object as input. If it does not find the object you are passing in, it will return -1. You need to pass the object whose location in the arraylist you are looking for as the input into the indexOf function.
Solution :
create a HelperClass to pass into the indexOf method:
.indexOf(new HelperClass(name, email, phoneno));
However that change by itself will still return -1. See the api doc for indexOf:
public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in
this list, or -1 if this list does not contain the element. More
formally, returns the lowest index i such that (o==null ? get(i)==null
: o.equals(get(i))), or -1 if there is no such index.
It's using equals to decide whether it's found a match. You should have overridden the equals method on your HelperClass class, so it's using the default implementation in java.lang.Object, which compares the references, and only returns true if the two references HelperClass to the same object.
Override equals and hashcode on your HelperClass class, like:
#Override public boolean equals(Object other) {
if (!(other instanceof HelperClass)) {
return false;
}
HelperClass otherHelperClass = (HelperClass)other;
return otherHelperClass.x == this.x && otherHelperClass.y == this.y;
}
#Override public int hashCode() {
return x + y; // same values should hash to the same number
}

Collection.contains yields wrong result

I've got an object which has got a couple of fields -- as you can see the hashcode and equals method is implemented just taking the id in account:
public class SpotResponse{
String id;
// bla bla other fields
public SpotResponse() {
}
public SpotResponse(#NonNull String id) {
this.id = id;
}
public String getId() {
return id;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SpotResponse that = (SpotResponse) o;
return id == that.id;
}
#Override
public int hashCode() {
return id.hashCode();
}
}
I've got a method which checks if a Collection<SpotResponse> newSpots
contains some oldSpots from a HashMap<String, SpotResponse> spots = new HashMap<>();
If I do this:
List<String> newKeys = new ArrayList<>();
for (SpotResponse response : newSpots) {
newKeys.add(response.getId());
}
for (SpotResponse oldSpot : spots.values()) {
if (newKeys.contains(oldSpot.getId())) {
continue;
}
/* blabla */
}
newKeys.contains() returns true properly, but if instead I do
newSpots.contains(oldSpot)
It always returns false. In this case, the Collection is an ArrayList (if this is of any help)
Your bug is in your equals implementation, on this line:
return id == that.id;
You're comparing two Strings (namely id and that.id) with ==, when you should use id.equals(that.id).
You have to be careful when using Contains with Primitives.
String is technically an object class wrapping a primitive of chars, but when you are comparing it, it is not comparing the literal object memory pointer, it is comparing the value at the memory pointer.
Contains is using .equals under the hood, so when overriding equals in your class you can't default back to the == comparison as that compares address and not necessarily value.
Hope that helps.

Modify existing Key of HashMap In Java

I'm working with HashMap since few days, and facing below weird situation.
Case 1:Changed Key which is already existing in HashMap, and print HashMap
Case 2: Changed key which is already existing in HashMap and Put that key again into the HashMap. Print HashMap.
Please find below code as well as two different output of two case.
Could you please anyone let me know, whats going on in below code.
import java.util.HashMap;
import java.util.Set;
class Emp{
private String id ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Emp(String id) {
super();
this.id = id;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Emp other = (Emp) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
#Override
public String toString() {
return "Emp [id=" + id + "]";
}
}
public class HashMapChanges {
public static void main(String[] args) {
// TODO Auto-generated method stub
Emp e1 = new Emp("1");
Emp e2 = new Emp("2");
Emp e3 = new Emp("3");
Emp e4 = new Emp("4");
HashMap<Emp, String> hm = new HashMap<Emp,String>();
hm.put(e1,"One");
hm.put(e2,"Two");
hm.put(e3,"Three");
hm.put(e4,"Four");
e1.setId("5");
/** Uncomment below line to get different output**/
//hm.put(e1,"Five-5");
Set<Emp> setEmpValue = hm.keySet();
for(Emp e : setEmpValue){
System.out.println("Key"+ e +" Value "+ hm.get(e));
}
}
}
Output of above code :
KeyEmp [id=2] Value Two
KeyEmp [id=5] Value null
KeyEmp [id=4] Value Four
KeyEmp [id=3] Value Three
Output After uncommenting line
KeyEmp [id=5] Value Five-5
KeyEmp [id=2] Value Two
KeyEmp [id=5] Value Five-5
KeyEmp [id=4] Value Four
KeyEmp [id=3] Value Three
Using mutable objects as keys in a Map is not permitted when the key used to determine its location in the Map is mutable.
From the Javadoc for java.util.Map<K,V>:
Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map.
You are violating the contract required of map keys because your Emp object is mutable, and the change modifies the attribute used to determine where in the map the key resides.
The result is undefined behavior.
I suspect you have misunderstood the Map concept, based on your code, but without understanding what you're actually trying to achieve we really cannot help further. I suggest you ask a new question explaining your actual goals.
You overwrite the hashCode() and equals() method ,
then ,the Map's key is the hashCode result.
then id=1 and id=5 are two different items.
You can comment the two methods and try again.

HashSet: Cannot Remove An Object That Is Equal?

In my code, I am trying to remove an element in a HashSet. I have overridden the equals method in my object.
for (BitVector bv : implicantsToDeleteViaBitVector) {
System.out.println(bv + " , " + bv.hashCode());
for(BitVector bvSetbv : bvSet){
if(bv.equals(bvSetbv)){
System.out.println("match " + bv);
}else{
System.out.println("No match " + bv + "," + bvSetbv);
}
}
if(bvSet.remove(bv)){
System.out.println("Remove");
}else{
System.out.println("No match found by remove");
}
...
The problem is that my equals method will return true but my remove returns false on the same item being examined. This is the equals method:
#Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof BitVector))
return false;
BitVector v = (BitVector) o;
boolean sameSize = mySize == v.getSize();
boolean sameTerms = this.bitVectorList
.containsAll(v.getBitVectorList());
return (sameSize && sameTerms);
}
Does remove use the equals() method in a class? If so, then why is this code not working?
You state:
I have overridden the equals method in my object.
You need to also override the hashCode() method, and its contract should be that if two objects are equal, then their hashCodes also must be equal, although the converse is not necessarily true in that it is possible and allowed for two objects to have the hash code and not be equal, although this is something that should be avoided if possible.
One of the possible problems is a mutable object (more accurately mutable hashcode). When you add an object to HashSet hashcode will be calculated only once. So if you change the object and try to 'remove' the SAME object from the collection the object will not be deleted.
By overriding hasCode and equal method following way, you can fix your problem
public int hashCode(){
System.out.println("In hashcode");
int hashcode = 0;
hashcode = price*20;
hashcode += item.hashCode();
return hashcode;
}
public boolean equals(Object obj){
System.out.println("In equals");
if (obj instanceof Price) {
Price pp = (Price) obj;
return (pp.item.equals(this.item) && pp.price == this.price);
} else {
return false;
}
}
public String toString(){
return "item: "+item+" price: "+price;
}

Comparing two objects using an equals method, Java

I have an array of objects that I want to compare to a target object. I want to return the number of objects that exactly match the target object.
Here is my count method:
public int countMatchingGhosts(Ghost target) {
int count=0;
for (int i=0;i<ghosts.length;i++){
if (ghosts[i].equals(target));
count++;
}
return count;
And here is my equals method:
public boolean equals(Ghost other){
if(this == other) return true;
if( !(other instanceof Ghost) ) return false;
Ghost p = (Ghost)other;
if (this.x == p.x && this.y == p.y && this.direction==p.direction && this.color.equals(p.color))
return true;
else
return false;
I run some test code, and I expect 1 matching only, but I get 3 instead. Do you see any errors?
There is a ; at the end of your if:
if (ghosts[i].equals(target));
^
This makes count++; happen always irrespective of what your equals method returns.
You should override this function:
public boolean equals(Object other) { }
Do note the Object class being used in method's signature instead of Ghost. Your can use #Override annotation to get a compiler error if you are not using method signature correctly.
#Override
public boolean equals(Object other) { }
Having said that, what's probably happening in your code is what the other answer is stating...
Just thought I add that while implementing the equals method in your code, you must also implement (override) the hashCode method. This is a general contract that you must follow for the best performances.
Below is an excerpt from Joshua Bloch's book "Effective Java"
Item 9: Always override hashCode when you override equals
A common source of bugs is the failure to override the hashCode method. You
must override hashCode in every class that overrides equals. Failure to do so
will result in a violation of the general contract for Object.hashCode, which will
prevent your class from functioning properly in conjunction with all hash-based
collections, including HashMap,HashSet, and Hashtable.
Here is the contract, copied from the Object specification [JavaSE6]:
• Whenever it is invoked on the same object more than once during an execution
of an application, the hashCode method must consistently return the
same integer, provided no information used in equals comparisons on the
object is modified. This integer need not remain consistent from one execution
of an application to another execution of the same application.
• If two objects are equal according to the equals(Object) method, then calling
the hashCode method on each of the two objects must produce the same
integer result.
And just like Pablo said, if you use anything other than the Object class in your equals method signature, you aren't actually overriding the equals method, and your program won't work as expected.
Take for example this small program that copies a List to a Set(which cannot contain duplicates) and prints the new Collection. Try swapping equals(Object obj) with equals(Item obj) and see what happens when you run the program. Also, comment out the hashCode() method and run the program and observe the difference between using it and not.
public class Item {
private String name;
private double price;
private String countryOfProduction;
public Item(String name, double price, String countryOfProduction) {
this.setName(name);
this.setPrice(price);
this.setCountryOfProduction(countryOfProduction);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCountryOfProduction() {
return countryOfProduction;
}
public void setCountryOfProduction(String countryOfProduction) {
this.countryOfProduction = countryOfProduction;
}
public String toString() {
return "Item Name: " + getName() + "\n" +
"Item Price: N" + getPrice() + "\n" +
"Country of Production: " + getCountryOfProduction() + "\n";
}
#Override
public boolean equals(Object obj) {
if(!(obj instanceof Item)) {
return false;
}
if(obj == this) {
return true;
}
Item other = (Item)obj;
if(this.getName().equals(other.getName())
&& this.getPrice() == other.getPrice()
&& this.getCountryOfProduction().equals(other.countryOfProduction)) {
return true;
} else {
return false;
}
}
public int hashCode() {
int hash = 3;
hash = 7 * hash + this.getName().hashCode();
hash = 7 * hash + this.getCountryOfProduction().hashCode();
hash = 7 * hash + Double.valueOf(this.getPrice()).hashCode();
return hash;
}
public static void main (String[]args) {
List<Item> items = new ArrayList<>();
items.add(new Item("Baseball bat", 45, "United States"));
items.add(new Item("BLUESEAL Vaseline", 1500, "South Africa"));
items.add(new Item("BLUESEAL Vaseline", 1500, "South Africa"));
Collection<Item> noDups = new HashSet<>(items);
noDups.stream()
.forEach(System.out::println);
}
}

Categories

Resources