List of attributes with differences between objects - Java

Asked

Viewed 727 times

0

I need to compare two objects in JAVA so I can get a MAP with key and value of the attributes that present difference. I need this logic for a generic object, capable of receiving 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

In this example would have a return of a Map, or other data structure as follows:

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

Does anyone know in any way that I might be doing this? Java has some function that already makes this difference?

Thank you!

1 answer

1


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?

  • I updated my answer. See if this is what you’re looking for?

  • 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.

  • 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)

  • 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.