简体   繁体   English

在Java中,如何比较两个对象的值?

[英]In Java, how can I compare the values of two objects?

I want to build and IP Address management tool. 我要构建和IP地址管理工具。 I want to store used IPs in a database with information on the device that uses the IP. 我想将使用过的IP信息与使用该IP的设备上的信息一起存储在数据库中。 I want to be able to then pull the "used" ips out and compare them against a list of IPs from a given subnet and determine if the ip is "used" or "available". 然后,我希望能够拉出“已使用”的ip,并将它们与给定子网中的IP列表进行比较,并确定该IP是“已使用”还是“可用”。 I need the "Available" list to generate a list I can choose from to add to new devices, without picking a "used" ip. 我需要“可用”列表来生成一个列表,我可以从中选择要添加到新设备的列表,而无需选择“已用” ip。

Here is what I tried but it doesnt' seem to be comparing the two objects of type IPAddress. 这是我尝试过的方法,但似乎并没有比较IPAddress类型的两个对象。

    ArrayList<IPAddress> used = new ArrayList<>();
    ArrayList<IPAddress> available = new ArrayList<>();
    ArrayList<IPAddress> subnet = new ArrayList<>();

    //I reference my IPAddress class which has four int fields:
    //oct_1, oct_2, oct_3, oct_4  
    //the constructor for IPAddress takes for Ints and I build my IPAddress
    //from the for octets.

    //build a list of all ips in the subnet 10.50.2.0 - 10.50.2.255
    //assuming a class C subnet 255.255.255.0
    for(int i = 0; i < 256; i++){
        subnet.add(new IPAddress(10,50,2,i));
    }

    //identify used ips.  Eventually want to pull these from a database
    //but for now these are just random IP's representing possible used IPs
    used.add(new IPAddress(10,50,2,3));
    used.add(new IPAddress(10,50,2,5));
    used.add(new IPAddress(10,50,2,9));
    used.add(new IPAddress(10,50,2,13));
    used.add(new IPAddress(10,50,2,17));
    used.add(new IPAddress(10,50,2,22));


    //**** NEEDED CODE ********
    //i want to iterate through each IP in the subnet and check if it's in
    //the 'used' list.  If it IS NOT add the ip to the 'available' list.

    //my failed try      
    for(IPAddress ip : subnet){
        if(!used.contains(ip)){ //if ip is NOT in used add it to available
            available.add(ip);
        }
    }



    //print results out to the screen          
    for(IPAddress ip : available){
        System.out.println(ip.toString() + " is available.");
    }       

    for(IPAddress ip: used){
        System.out.println(ip.toString() + " is used.");
    }




//********************  Here is my IPAddress Class if it helps ****************
public class IPAddress {
private int oct_1 = 0;
private int oct_2 = 0;
private int oct_3 = 0;
private int oct_4 = 0;

public IPAddress(int Oct_1, int Oct_2, int Oct_3, int Oct_4){
   oct_1 = Oct_1;
   oct_2 = Oct_2;
   oct_3 = Oct_3;
   oct_4 = Oct_4;
}

@Override
public String toString(){
    String ipAddress = "";

    if(getOct_1() != 0){
    ipAddress = getOct_1() + "." + getOct_2() + "." + getOct_3() + "." + getOct_4();
           }
    return ipAddress;

}

The contains method will use equals . contains方法将使用equals Make sure that you override the equals method in your IPAddress class. 确保覆盖IPAddress类中的equals方法。

Additionally, the Javadocs for equals state that the hashCode method should be overridden whenever equals is overridden. 此外, Javadocs for equals声明,只要覆盖equals就应该覆盖hashCode方法。

Indicates whether some other object is "equal to" this one. 指示其他某个对象是否与此对象“相等”。

and

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. 请注意,通常有必要在每次重写此方法时都重写hashCode方法,以维护hashCode方法的常规约定,该约定规定相等的对象必须具有相等的哈希码。

You have to override the equals method (from the Object class) and define what it means for two IPAddresses to equal one another. 您必须重写equals方法(来自Object类),并定义两个IPAddress彼此相等意味着什么。

Also, if two objects equal one another they have to have the same hashcode, so override the hashcode method too. 同样,如果两个对象彼此相等,则它们必须具有相同的哈希码,因此也要覆盖该哈希码方法。

You can write a custom comparer that matches the property of two object and return boolean or override the equals method of object. 您可以编写一个与两个对象的属性匹配的自定义比较器,然后返回布尔值或覆盖对象的equals方法。

@Override
public boolean equals(Object other){
    if (other == null) return false;
    if (other == this) return true;
    if (!(other instanceof IPAddress))return false;
    IPAddress otherIPAddress = (IPAddress)other;
     //match other properties and return result
     return otherIPAddress.oct_1 == this.oct_1 && otherIPAddress.oct_2 == this.oct_2 && otherIPAddress.oct_3 == this.oct_3 && otherIPAddress.oct_4 == this.oct_4;
}

I'd also like to add that you should always override hashCode whenever overriding equals . 我还想补充一点, 无论何时重写equals ,都应始终重写hashCode

You need to implement the equals method for the class IPAdress. 您需要为IPAdress类实现equals方法。 Ideally you use a HashSet instead of a list and Implement the hashCode method in the IPAddress class for efficiency. 理想情况下,您使用HashSet代替列表,并在IPAddress类中实现hashCode方法以提高效率。

The == operator will determine equality by seeing if two objects hold the same location in memory. ==运算符将通过查看两个对象在内存中是否位于同一位置来确定相等性。

The .equals function returns true if the passed object is "equal" to the current object. 如果传递的对象与当前对象“相等”,则.equals函数将返回true。 Override .equals to determine two objects are equal by your own logic. 根据您自己的逻辑,重写.equals以确定两个对象是否相等。 The .hashcode function should also be overridden if you override .equals . 如果覆盖.equals也应该覆盖.hashcode函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM