0

I want to build and IP Address management tool. I want to store used IPs in a database with information on the device that uses the 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". I need the "Available" list to generate a list I can choose from to add to new devices, without picking a "used" ip.

Here is what I tried but it doesnt' seem to be comparing the two objects of type 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;

}
MBFROMIT
  • 13
  • 1
  • 5
  • @RC.: that won't help solve the problem. He wants a simple contains or not, not a ranking or sorting. – Hovercraft Full Of Eels Jan 07 '14 at 18:48
  • If you created this IPAddress type yourself, I think you need to define the equals method to tell it how to do the comparison. I would have just used a String to represent the IPs. See http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java – developerwjk Jan 07 '14 at 18:53

5 Answers5

5

The contains method will use equals. Make sure that you override the equals method in your IPAddress class.

Additionally, the Javadocs for equals state that the hashCode method should be overridden whenever equals is overridden.

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

You have to override the equals method (from the Object class) and define what it means for two IPAddresses to equal one another.

Also, if two objects equal one another they have to have the same hashcode, so override the hashcode method too.

Ben
  • 346
  • 1
  • 8
0

You can write a custom comparer that matches the property of two object and return boolean or override the equals method of object.

@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.

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • This seems to be the most thourogh answer, but Im not sure how to override hashcode. So I chose to implement my own method. – MBFROMIT Jan 07 '14 at 19:40
0

You need to implement the equals method for the class IPAdress. Ideally you use a HashSet instead of a list and Implement the hashCode method in the IPAddress class for efficiency.

fatih
  • 1,395
  • 10
  • 9
0

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. Override .equals to determine two objects are equal by your own logic. The .hashcode function should also be overridden if you override .equals.

William Morrison
  • 10,953
  • 2
  • 31
  • 48