当前位置: 动力学知识库 > 问答 > 编程问答 >

hashtable - Hashcode for a class with 2 different fields Java

问题描述:

I can't seem to find a solution for this problem:

a.example 1.1.1.1

a.example 1.2.1.1

where first column is the hostname and the second one is the ip.

These two information are used to compose an object of a class Router.java. I want to use objects of Router.java as a key to a Hashmap and Boolean as value i.e. HashMap < Router,Boolean >.

The way I want to use my ".containsKey" method is that it checks if one of the fields (either hostname or ip) are the same , then it should return true. Also in such a case I want to make the value corresponding to such an Object of Router.java askey in the hashmap as true.

My problem is that I don't know how to write the hash method in order for the two objects to have the same hash and go to the .equals().

 import java.util.Objects;

`enter code here`public class Router {

String hostname;

String ip_address;

String patched;

String os_version;

String notes;

public Router(String hostname,String ip_address,String patched,String os_version,String notes)

{

this.hostname = hostname;

this.ip_address = ip_address;

this.patched = patched;

this.os_version = os_version;

this.notes = notes;

}

@Override

public boolean equals(Object o) {

if (o == this)

return true;

if (!(o instanceof Router)) {

return false;

}

Router r = (Router) o;

return r.hostname.equals(hostname) ||

r.ip_address.equals(ip_address) ;

}

@Override

public int hashCode() {

}

}

网友答案:

I feel finding a way to fix a problem temporarily while having the wrong base is very bad practice. The equals method is wrongly implemented. It contradicts the contract and hence there are chances that code will have many issues later.

Suppose there are 3 objects of Router.

  1. Object1 : routerA has hostname = a.abc and ip = 107.108.109.100

  2. Object2 : routerB has hostname = a.abc and ip = 107.108.109.200

  3. Object3 : routerC has hostname = b.abc and ip = 107.108.109.100

    routerA.equals(routerB); // Evaluates to TRUE

    routerA.equals(routerC); // Evaluates to TRUE

    routerB.equals(routerC); // Should Evaluates to TRUE but Evaluates to FALSE which is wrong as per equals contract.

Refer : https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

  1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.

  2. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

  3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.

  4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.

  5. For any non-null reference value x, x.equals(null) should return false

You may have issues with using Objects of Router in Collection Objects and also while sorting you may get unexpected results. Also while working ahead the code will have huge maintaince issues, any one looking at your code will have difficulty in understading the logic (which is actually wrong in this case). A new comer to a project with such code base will have nightmares.

Refactoring is a good habbit, we all make mistakes but better we learn, realize and correct them and do not move ahead with them.

I wish you will correct your logic and implement equals and hashcode as per the contract set for these methods. Good luck and cheers.

[Update] Also I see an answer by @RustyX below, (http://stackoverflow.com/a/42057421/504133 ) in your case you can use 2 hashmaps, one with IP as key and boolean as value, and 2nd hashmap with hostname as key and boolean as value. While checking for a Router, you can check if its IP is present in Hashmap with IP as key or if hostname is preaent jn hashmap with hostname as key. While putting the result you need to see you do not put conflicting values in 2 maps. With little carefully written methods and checks hopefully you will be able to get a solution to your problem. Note that as mentioned by RustyX im this case you may not need to implement equals and hashcode for atleast handling this case.

网友答案:

You want to search on two independent data items.

So use two hash maps, one for addresses and one for IPs. Store the same object as the value in both of them.

You don't need to implement any hashCode.

分享给朋友:
您可能感兴趣的文章:
随机阅读: