-1

I have 3 fields for defining a unique object, e.g.: region, name, platform which are string values. A list of such objects has to be compared with another list of such objects. I was thinking to create an object for that such as

Class myObject{    
  private String region;    
  private String name;    
  private String platform;    
}

and then create a list of them to compare each objects in both the lists. Somebody please provide me a better solution for this problem.

I have two tables with columns, id,region, platform, name, zone, count ,etc and the values repeat in this table. Another table has id, region, platform, name zone. First table gives the list of reserved AWS EC2 instances and second table the list of AWS EC2 instances which are running now. I need to find out if all the reserved instances are currently running or is there anything unutilized.

Can anyone suggest a good solution for this problem.

mattdave
  • 37
  • 9
  • You can override the `equals()` providing the various checks method then pretty much use a for loop to check `for(MyObject ob1 : list1) { for(MyObject ob2 : list2) { if(ob1.equals(ob2))}}` depending on what you want from the two lists – Kenneth Clark Jun 01 '15 at 11:06

3 Answers3

1

You need to extend your class definition with and equals() and hashCode() method.

See the following articles: Equality on artima.com & Java Equals & HashCode on ideyatech.com. Google for many more.

You can then use a Set or iterate through a list using equals as the test to help generate a collection of unique objects.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
  • Thanks for your quick response. I am editing my problem and will elaborate it. I need to know if there are workarounds for this particular problem – mattdave Jun 01 '15 at 11:28
0

To check if the two objects are equal, you need to override equals() and should override hashCode() also. More detail can be found : here

Below is a sample class MyClazz with a test MyClazzTest to help you understand:

MyClazz

package com.my.test;

public class MyClazz {

    private String region;
    private String name;
    private String platform;

    public MyClazz(String region, String name, String platform) {
        this.region = region;
        this.name = name;
        this.platform = platform;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result
                + ((platform == null) ? 0 : platform.hashCode());
        result = prime * result + ((region == null) ? 0 : region.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;
        MyClazz other = (MyClazz) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (platform == null) {
            if (other.platform != null)
                return false;
        } else if (!platform.equals(other.platform))
            return false;
        if (region == null) {
            if (other.region != null)
                return false;
        } else if (!region.equals(other.region))
            return false;
        return true;
    }
}

MyClazzTest

package com.my.test;

import org.junit.Assert;
import org.junit.Test;

public class MyClazzTest {

    @Test
    public void equalObject() {
        MyClazz object = new MyClazz("UK", "Chris", "Window");
        MyClazz duplicateObject = new MyClazz("UK", "Chris", "Window");
        Assert.assertTrue(object.equals(duplicateObject));
    }

    @Test
    public void notEqualObject() {
        MyClazz object = new MyClazz("UK", "Chris", "Window");
        MyClazz differentObject = new MyClazz("US", "Chris", "Window");
        Assert.assertFalse(object.equals(differentObject));
    }
}
Community
  • 1
  • 1
TheCodingFrog
  • 3,406
  • 3
  • 21
  • 27
  • Thank you for your descriptive answer. Can you help me figure out a better solution for this particular problem, thanks in advance. I've editedmy question – mattdave Jun 01 '15 at 11:36
0

For the edited question:

  1. Create a POJO Class with all fields, representing a composite key.
  2. Implement equals and hashCode methods for it.
  3. Create 2 collections of POJOs, filling them with data from tables.
  4. Use one of the methods of CollectionUtils of ApacheCommons to play with your collections. E.g.: CollectionUtils#isSubCollection
hammelion
  • 351
  • 2
  • 13