0

Supposed i have 2 json files and i have 2 JSONObject-s (1 to represent each file) and the files represent the same data but in DIFFERENT ORDER of the blocks in the file.

for example:

file1.json

name: {

    entry1
    entry3
    entry2

}

file2.json

name: {

    entry2
    entry1
    entry3

}

Where entry-i in each file is the same.

How can i perform equals on them even though the order is different ?

Thanks

BackSlash
  • 21,927
  • 22
  • 96
  • 136
Caffeine
  • 113
  • 1
  • 1
  • 7

1 Answers1

0

Translate the whole JSON Model to Java Objects. In your example every "entry" simply could be a string. If it's more complex than you have to implement something like POJOs for that. Those POJOs should have equals and hashCode Methods (be aware of their contract)

Then simply put all the entries of every JSON into a HashSet (which doesn't have any order -> click) and compare the HashSets with equals to each other.

Jan
  • 1,004
  • 6
  • 23
  • Or you can just use one of the methods provided by JSON libraries instead of creating lots of objects and manually comparing them implementing your own equals method (which is both slow and memory consuming). – BackSlash Feb 26 '18 at 08:14
  • If he only wants to compare those things: yes that's the better way. Just expected he'll work with the objects afterwards. My fault ^^ – Jan Feb 26 '18 at 08:16