Created
December 4, 2019 07:14
-
-
Save Danushka96/d08f1e92aba4f674539836e85cc2853d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
/** | |
* @author danushka | |
*/ | |
public class test { | |
public static void main(String[] args) { | |
JsonArray arr1 = new JsonArray(); | |
JsonArray arr2 = new JsonArray(); | |
for (int i = 0; i < 5; i++) { | |
JsonObject jsonObject = new JsonObject(); | |
jsonObject.addProperty("id",i); | |
jsonObject.addProperty("name","a_"+i); | |
jsonObject.addProperty("age",i); | |
arr1.add(jsonObject); | |
} | |
for (int i = 3; i < 8; i++) { | |
JsonObject jsonObject = new JsonObject(); | |
jsonObject.addProperty("id",i); | |
jsonObject.addProperty("name","a_"+i); | |
jsonObject.addProperty("age",i); | |
arr2.add("{'name': 'abc'}"); | |
arr2.add(jsonObject); | |
} | |
JsonArray result = intersection(arr1, arr2, "id", "id"); | |
result.forEach(System.out::println); | |
} | |
private static JsonArray intersection(JsonArray arr1, JsonArray arr2, String columnName1, String columnName2){ | |
JsonArray result = new JsonArray(); | |
arr1.forEach((element1) -> { | |
if(element1.getAsJsonObject().has(columnName1)){ | |
arr2.forEach((element2) -> { | |
if (element2.getAsJsonObject().has(columnName2)){ | |
if(element1.getAsJsonObject().get(columnName1).equals(element2.getAsJsonObject().get(columnName2))){ | |
result.add(element2); | |
} | |
} | |
}); | |
} | |
}); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment