List with attributes that present difference between objects - Java

0

I need to compare two objects in JAVA so I can get a MAP with key and value of the attributes that have difference. I need this logic for a generic object, able to receive any type.

Ex:

OBJETO 1 - nome: João, email: [email protected], telefone: 1234-5678, endereco: rua123

OBJETO 2 - nome: João, email: [email protected], telefone: 1234-5678, endereco: rua678

This example would have a return from a Map, or another data structure as follows:

{email: [email protected], endereco: rua 678}

Does anyone know of any way I can be doing this? Does Java have some function that already makes this difference?

Thank you!

    
asked by anonymous 02.01.2018 / 20:07

1 answer

1

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()));
}

}
    
02.01.2018 / 20:11