You can define a custom Comparator in your data structure and then implement the comparison between the two objects.
Update:
I thought you wanted to sort through a data structure based on your differences, but I guess that’s not what you want to do. As I understand it best you just want to get the attribute and its value from the different fields of two objects. I believe that there is nothing specific that Java offers. You can develop something like this:
public class Teste {
static class Pessoa {
private String nome;
private String email;
private String telefone;
private String endereco;
public Pessoa(String nome, String email, String telefone, String endereco) {
this.nome = nome;
this.email = email;
this.telefone = telefone;
this.endereco = endereco;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
}
public static Map<String, String> getDiffs(Pessoa p1, Pessoa p2) {
Map<String, String> diffs = new HashMap<>();
if (!p1.getNome().equals(p2.getNome())) {
diffs.put("nome", p2.getNome());
}
if (!p1.getEmail().equals(p2.getEmail())) {
diffs.put("email", p2.getEmail());
}
if (!p1.getTelefone().equals(p2.getTelefone())) {
diffs.put("telefone", p2.getTelefone());
}
if (!p1.getEndereco().equals(p2.getEndereco())) {
diffs.put("endereco", p2.getEndereco());
}
return diffs;
}
public static void main(String[] args) {
Pessoa p1 = new Pessoa("João", "[email protected]", "1234-5678", "rua123");
Pessoa p2 = new Pessoa("João", "[email protected]", "1234-5678", "rua678");
Map<String, String> diffs = getDiffs(p1, p2);
System.out.println(Arrays.toString(diffs.entrySet().toArray()));
}
}
Update2:
Using Reflection.
public class Test {
static class Pessoa {
private String nome;
private String email;
private String telefone;
private String endereco;
public Pessoa(String nome, String email, String telefone, String endereco) {
this.nome = nome;
this.email = email;
this.telefone = telefone;
this.endereco = endereco;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
}
public static Map<String, Object> getDiffs(Pessoa p1, Pessoa p2) throws IllegalArgumentException, IllegalAccessException {
Map<String, Object> diffs = new HashMap<>();
for (Field field : p1.getClass().getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object fieldValue1 = field.get(p1);
Object fieldValue2 = field.get(p2);
if (!fieldValue1.equals(fieldValue2)) {
diffs.put(fieldName, fieldValue2);
}
}
return diffs;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
Pessoa p1 = new Pessoa("João", "[email protected]", "1234-5678", "rua123");
Pessoa p2 = new Pessoa("João", "[email protected]", "1234-5678", "rua678");
Map<String, Object> diffs = getDiffs(p1, p2);
System.out.println(Arrays.toString(diffs.entrySet().toArray()));
}
}
Would you have any example that could help me?
– Pedro
I updated my answer. See if this is what you’re looking for?
– Patrícia Espada
That’s exactly it, but I need something generic, due to the fact that I might be able to compare several different types of objects. And to implement this difference in every model that I have, it would be something very painful, also having a lot of code redundancy.
– Pedro
For this case the only way I know is using Reflection. You have to take into account the drawbacks of using this approach and so I advise you to read a little bit about it. (I updated the example)
– Patrícia Espada
That’s what I needed, but I was able to do otherwise, using the classes (Diff, Diffresult, Diffbuilder) of the Builder package of Apache Lang 3. But I used Reflection as exemplified here. Thank you!
– Pedro