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

时间:2014-01-07 18:45:54

标签: java arraylist compare

我想构建和IP地址管理工具。 我想将已使用的IP存储在数据库中,其中包含有关设备的信息 使用IP。我希望能够将“使用过的”ips拉出来并进行比较 针对来自给定子网的IP列表,并确定ip是“已使用”还是“可用”。 我需要“可用”列表来生成一个列表,我可以选择添加到新设备,而无需选择“已使用”的IP。

这是我尝试的但它似乎并没有比较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;

}

5 个答案:

答案 0 :(得分:5)

contains方法将使用equals。请务必覆盖equals课程中的IPAddress方法。

此外,Javadocs for equals表示,只要覆盖hashCode,就应覆盖equals方法。

  

指示某个其他对象是否“等于”此对象。

  

请注意,通常需要在重写此方法时覆盖hashCode方法,以便维护hashCode方法的常规协定,该方法声明相等的对象必须具有相同的哈希代码。

答案 1 :(得分:1)

您必须覆盖equals方法(来自Object类)并定义两个IP地址相互相等的含义。

此外,如果两个对象彼此相等,则它们必须具有相同的哈希码,因此也要覆盖哈希码方法。

答案 2 :(得分:0)

您可以编写一个与两个对象的属性匹配的自定义比较器,并返回boolean或覆盖对象的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;
}

我还想补充一点,你应该always override hashCode whenever overriding equals

答案 3 :(得分:0)

您需要为类IP地址实现equals方法。理想情况下,您使用HashSet而不是列表,并在IP地址类中实现hashCode方法以提高效率。

答案 4 :(得分:0)

==运算符将通过查看两个对象是否在内存中保持相同位置来确定相等性。

如果传递的对象与当前对象“相等”,则.equals函数返回true。覆盖.equals以确定两个对象与您自己的逻辑相同。如果您覆盖.hashcode,则还应覆盖.equals函数。