List Filter in Java

Asked

Viewed 1,216 times

0

I am filtering one ArrayList<ArrayList<>> which I have to check whether certain data have already been entered inside it, detailed form:

int index = produtos.indexOf(produtos.stream().filter((x) -> x.get(0).Nome == prod.Nome).findFirst());

the list composes lists that are lists of products, each product list should contain the same product record, so I’m trying to find the product name on ArrayList<ArrayList<produto>>. But the index comes -1, as if not hearing the found

  • You have a list, this list has lists of products, and you want to find the list that has the desired product?

  • It seems an XY problem, you do not wish to have duplicates?

1 answer

0


Your code has a major problem which is the comparison of Strings, you can read more about it in that matter.

Anyway, there are better ways to do what you need:

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import lombok.Data;
import lombok.RequiredArgsConstructor;

public class Main {

    public static void main(String[] args) {
        List<List<Produto>> listaDeListas = Arrays.asList(
                                                Arrays.asList(new Produto("Produto 1"), new Produto("Produto 2")),
                                                Arrays.asList(new Produto("Produto 3")),
                                                Arrays.asList(new Produto("Produto 4"), new Produto("Produto 5"), new Produto("Produto 6")),
                                                Arrays.asList(new Produto("Produto 7"))
                                            );
        // Condição desejada
        Predicate<Produto> filtroProduto = p -> p.getNome().equalsIgnoreCase("Produto 1");

        // Recupera a lista contendo o produto
        List<Produto> listaComOcorrencia = listaDeListas.stream()
                                            .flatMap(List::stream)
                                            .filter(filtroProduto)
                                            .collect(Collectors.toList());
        System.out.println(listaComOcorrencia);

        // Recupera a contagem de produtos com o critério
        long count = listaDeListas.stream()
                        .flatMap(List::stream)
                        .filter(filtroProduto)
                        .count();
        System.out.println(count);
    }

    @Data
    @RequiredArgsConstructor
    public static class Produto {
        private final String nome;
    }
}

The output of the program is:

[Main.Produto(nome=Produto 1)]
1

Where on the first stream, I retrieved the (s) list(s) containing the product, and the Agrupei.

Probably the second filter is the most interesting, since it only recovers a product count meeting that condition. You can then compare and see if the product has already been put using count != 0.

Anyway, if you DO NOT want duplicates, and do not want to bother comparing if the object already exists, you should use the structure Set, which does not allow duplicates

  • Hello, thank you for the answer! Apart from the direct comparison of strings I had done in my example so I understood the operation was the same, no? the only difference was the storage of the condition in a variable. However its method worked, would tell me why?

  • I think the biggest difference is in the code getting more readable, with a Count you have the total count information, which makes it easier while reading the code to identify that you would like to check if any of the items exists in the list. Does the index work? Yes! But the information during reading is not so clear at first glance, comparing if the index is != -1 for example

Browser other questions tagged

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