4
I have two lists that share a common value between them. I wanted these selected objects to be filtered so that it is added to another list
List<Objeto1> lista1 = new ListaElementos1().getElementos();
List<Objeto2> lista2 = new ListaElementos2().getElementos();
List<Objeto1> destino = new ArrayList<>();
for(Objeto1 item : lista1){
for(Objeto2 array: lista2){
if(array.getId() == item.getId()){
if(!destino.contains(item)){
destino.add(item);
}
}
}
}
That was the way I found to make the filter so that it met my need. But I don’t think this is the best way to do it (by having to run the two entire lists for each record).
Is there any way to do this without the need to create 2 loops?
The lists are equal?
– Taffarel Xavier
which I will compare not
– Walter Miranda
Unless you have some specific requirement that’s not in the question, you don’t have much to do. May have some restriction that could allow some optimization.
– Maniero
I thought of that question based on the attribute
Find
in C#. It accepts a lambda to filter these records and returns an object from the list that meets this filter. I don’t know how it does it in alist
, if it’s afor
within it or a more optimized way. Perhaps this method is just as efficient but with a high complexity of understanding. About the specific requirement, I get a Json that sends me which group the item belongs to (by id), and the other object is a list of attributes related to that group– Walter Miranda