1

I have a JSONArray of the form :

    [[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}], [{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]

I want to compare individual fields like in the above case since { "Country" : "IN", "count" : 10} is equal to { "Country" : "IN", "count" : 10} and { "Country" : "IN", "count" : 10} and { "Country" : "US", "count" : 20} is equal to { "Country" : "US", "count" : 20} and { "Country" : "US", "count" : 20}, i Should get a Match result.

However for a case like below, I should get a unmatch result as the count don't match.

[[{ "Country" : "IN", "count" : 45},{ "Country" : "US", "count" : 60}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}],
[{ "Country" : "IN", "count" : 10},{ "Country" : "US", "count" : 20}]]

I was able to put the data into a HashMap. But I am unable to find a way, how to compare.

myArray contains the above JSONArray.

int length = myArray.getJSONArray(0).length();
Map<String, Integer> cargo = new HashMap<>();
for (int j = 0; j < myArray.length(); j++) {
  for (int k = 0; k < myArray.getJSONArray(j).length(); k++) {
    String country = myArray.getJSONArray(j).getJSONObject(k).getString(DataConstants.COUNTRY);
    Integer count = myArray.getJSONArray(j).getJSONObject(k).getInt(DataConstants.COUNT);
    cargo.put(country, count);
  }

}

if (cargo.size() == length) {
  System.out.println("Data Matched !!");
  return true;
}

else
  System.out.println("Data Not Matched !!");
return false;

Thanks,

AYa
  • 421
  • 3
  • 9
  • 21
  • 3
    You will want to convert your JSON arrays to java objects and override equals and hashcode methods. – alayor Sep 06 '17 at 20:11
  • @alayor Is there some link /code you can point me to oveeride equals and hashcode methods ? – AYa Sep 06 '17 at 20:16
  • @Aya: There were four obvious keywords in alayor's comment. Typing "Java", "override", "equals", and "hashCode" into Google's (or Stack Overflow's) search field would have been both easier and faster than asking us to do it for you. – Kevin J. Chase Sep 06 '17 at 21:58
  • @KevinJ.Chase I know you are right but you are not obliged to respond. It's totally upto you. Sorry, if I may sound harsh. – AYa Sep 06 '17 at 22:18

2 Answers2

2

You can create a class named Country which you can use to save the data from your JSON array.

class Country {
 private String countryCode;
 private int count;

 @Override
 public boolean equals(Object obj) {
   // compare your properties
 }

 @Override
 public int hashCode() {
   // Calculate a int using properties
 }
}

Take a look at this tutorial about how to implement your equals and hashcode methods.

Then, you need to convert your JSON array into java objects. Take a look at this tutorial.

alayor
  • 4,537
  • 6
  • 27
  • 47
  • Why exactly do I need to implement `hashcode` method ?. I mean I can just have the equals method since I am concerned only with comparing the values after put in the country map. – AYa Sep 06 '17 at 21:14
  • 2
    Take a look at this answer: https://stackoverflow.com/a/2265637/3800170 – alayor Sep 06 '17 at 21:34
1

Create a Pojo object with two variables and override the equals method.

public class Country {

private String code;
private int count;
//getters, setters

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((code == null) ? 0 : code.hashCode());
    result = prime * result + count;
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Country other = (Country) obj;
    if (code == null) {
        if (other.code != null)
            return false;
    } else if (!code.equals(other.code))
        return false;
    if (count != other.count)
        return false;
    return true;
}

}

Now you can create a HashMap of POJO objects by updating your code.

Country country = null;
        for (int j = 0; j < myArray.length(); j++) {
              for (int k = 0; k < myArray.getJSONArray(j).length(); k++) {
                String country = myArray.getJSONArray(j).getJSONObject(k).getString(DataConstants.COUNTRY);
                Integer count = myArray.getJSONArray(j).getJSONObject(k).getInt(DataConstants.COUNT);
                country = new Country();
                country.setCode(country);
                country.setCount(count);
                cargo.put(country); //change cargo to take country objects
              }

            }

Once you have the list of POJOs, you can do equals, contains and all other fancy operation to know the match.

yogidilip
  • 790
  • 6
  • 21
  • I am converting `cargo` to take country objects like this `Map cargo = new HashMap<>(); final ObjectMapper mapper = new ObjectMapper(); final Country pojo = mapper.convertValue(cargo, Country.class);` and then calling as `cargo.putAll((Map extends String, ? extends Integer>) country);`. Is this correct ? – AYa Sep 06 '17 at 20:44
  • `Map cargo = new HashMap();`. You may just wanna go with List. `List cargo = new ArrayList();` – yogidilip Sep 06 '17 at 20:57
  • Thanks. Now, I can convert that without using jackson. Once I have it in hashmap `cargo`. I should just iterate over similar keys and check if they have the same value for matching. Right ? – AYa Sep 06 '17 at 21:04
  • I am just thinking what should I return in my original method that contains creation of HashMap of POJO objects. – AYa Sep 06 '17 at 21:07