Check if Java List has duplicate object attributes

Asked

Viewed 123 times

1

I need help creating a logic that checks whether certain attributes of an object, stored in a List<Object>, there are more than two times.

I’ve tried using the Set<E> to check whether it is possible to store the data, as repeated information is not allowed on it. However, the ID I need to check is repeated, repeats in a non-linear 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;
}
  • You need to know which ones are duplicated or simply not show more duplicates?

  • I need to know which ones are duplicated

2 answers

0

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 Voce passed in the list.

  • Sorry, friend, in the project where I am we use a version of Java incompatible with these features... But it is a great solution

  • Got it. I really enjoy working the data using stream api.

0

Since there is no way to use stream to filter the data, Voce can do so:

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 statistical methods that will return you the existent values of the attribute that you search for. It will not tell you if there are repetitions, but will return the number of cases where an attribute receives the same value.

Browser other questions tagged

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