Verify that Java List has duplicate object attributes

I need help to create a logic that checks if certain attributes of an object, stored in a List<Object>, exist more than twice.

I have already tried to use Set<E> to check if it is possible to store the data, since repeated information is not allowed in it. However, the ID I need to check if is repeated, repeats in a nonlinear order. My code for the attempt is as follows:

public List<Object> processaListaAuxiliar(List<Object> listaDrem) {
    List<Object> listaAux = new ArrayList<Object>();
    Set<Object> hashSet = new HashSet<Object>();
    
    for (Object obj : listaDrem) {
        if(obj instanceof LinhaExcel) {
            
            if(!hashSet.add(((LinhaExcel) obj).getUgEmitente())) {
                System.out.println("-----");
            } else {
                System.out.println("TESTE");
            }
            
        }
    }
    
    return listaAux;
}
Author: jotape, 2021-01-19

2 answers

You can use a stream to filter the attribute you want to check and print each of them in the list. Ex:

public class Teste{
        String nome;
        int idade;

        public Teste(String nome, int idade){
                this.nome = nome;
                this.idade = idade;
        }
}
import java.util.List;
import java.util.Arrays;

public class Teste2{
        public static void main(String args[]){
                Teste t1 = new Teste("a",1);
                Teste t2 = new Teste("b",2);
                Teste t3 = new Teste("a",2);

                List<Teste> lista = Arrays.asList(t1,t2,t3);

                lista.stream().filter(t -> t.nome.equals("a")).forEach(System.out::println);

        }
}

In this case it will print each object with the attribute that you have passed present in the list.

 0
Author: hinoago, 2021-01-19 23:33:23

Since you can't use stream to filter the data, you can do this:

import java.util.ArrayList;
import java.util.List;

public class Teste{
    private String nome;
    private int id;

    public Teste(String nome, int id){
        this.nome = nome;
        this.id = id;
    }

    static public List<Teste> pesquisarNome(List<Teste> l,String n){
        ArrayList<Teste> al = new ArrayList<>();

        for(Teste obj : l){
            if(obj.getNome().equals(n)){
                al.add(obj);
            }
        }
        return al;
    }

    static public List<Teste> pesquisarId(List<Teste> l,int i){
                ArrayList<Teste> al = new ArrayList<>();

                for(Teste obj : l){
                        if(obj.getId() == i){
                                al.add(obj);
                        }
                }
                return al;
        }

    public String getNome(){
        return nome;
    }

    public int getId(){
        return id;
    }

    @Override
    public String toString(){
        return String.format("Nome: %s / Id: %d",getNome(),getId());
    }
}
import java.util.List;
import java.util.Arrays;

public class Teste2{
    public static void main(String args[]){
        Teste t1 = new Teste("a",1);
        Teste t2 = new Teste("b",2);
        Teste t3 = new Teste("c",3);
        Teste t4 = new Teste("a",2);

        List<Teste> lista = Arrays.asList(t1,t2,t3,t4);
        String nomePesquisa = "a";
        int idPesquisa = 2;

        List<Teste> repetidosNome = Teste.pesquisarNome(lista,nomePesquisa);
        List<Teste> repetidosId = Teste.pesquisarId(lista,idPesquisa);

        repetidosNome.forEach(System.out::println);
        System.out.println("--------------------------");
        repetidosId.forEach(System.out::println);
    }
}

In this example I created two static methods that will return you the existing values of the attribute you search. It will not tell you if there are repetitions, but rather return the number of cases in which an attribute receives the same value.

 0
Author: hinoago, 2021-01-20 16:10:22