You can define a custom comparator in your data structure and implement the comparison between the two objects.
Update:
I thought I wanted to sort into a data structure based on your differences, but that does not seem to be what you want to do. As I understand it, it's best to just get the attribute and its value from the different fields of two objects. I believe 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()));
}
}