Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter directly a collection based on a value?

I would like to know how can I get all the elements from a collection containing a specific value.

Like this:

@Override
public Collection<Sale> selectSales(String map) {
    HashSet<Sale> sales = new HashSet();

    for (Sale sale : salesList) {
        if (sale.getMap().equals(map)) {
            sales.add(sale);
        }
    }
    return sales;
}

But I would like to filter the collection directly. I read that I can do this using LAMBDA, example:

list.removeIf(c -> c.getCarColor() == Color.BLUE);

But I Don't know how to apply this example.

Thank you.

like image 453
A.BC Avatar asked Jan 04 '19 19:01

A.BC


People also ask

How will you run a filter on a collection?

You can filter Java Collections like List, Set or Map in Java 8 by using the filter() method of the Stream class. You first need to obtain a stream from Collection by calling stream() method and then you can use the filter() method, which takes a Predicate as the only argument.

How do you filter a condition in Java?

The filter() function of the Java stream allows you to narrow down the stream's items based on a criterion. If you only want items that are even on your list, you can use the filter method to do this. This method accepts a predicate as an input and returns a list of elements that are the results of that predicate.

How do you filter data in an ArrayList?

ArrayList removeIf() method in Java The removeIf() method of ArrayList is used to remove all of the elements of this ArrayList that satisfies a given predicate filter which is passed as a parameter to the method.

How do I filter the values in a column?

You can choose from three methods to filter the values in your column: After you apply a filter to a column, a small filter icon appears in the column heading, as shown in the following illustration. In the column header, you'll see an icon with an inverse triangle. When you select this icon, the sort and filter menu is displayed.

What are the different ways of filtering collections in Java?

In summary, we've seen that there are many different ways of filtering collections in Java. Even though Streams are usually the preferred approach, its good to know and keep in mind the functionality offered by other libraries. Especially if we need to support older Java versions.

Why can't I filter the items in my collection?

Because you are creating a collection you can shape on the ay in. Sometimes that can help, sometimes its not needed. But referring to another suggestion you have here, make sure there is no delegation issue with your collection creation either OnStart or however you trigger it. Otherwise the item will not be availbale to filter.

How to filter data based on cell values that contain text?

Filter Data Based on Cell Values that Contain Text Let’s assume that we have a dataset of products with their sales persons’ names, joining dates, and total sales. Now we will filter data based on the salesperson’s name. There are two additional functions used in the formula. They are ISNUMBER Click on the function’s name to learn more about them.


1 Answers

use a stream and filter:

salesList.stream()
         .filter(sale -> sale.getMap().equals(mapName)) // I've changed map to mapName as I am assuming that was a mistake
         .collect(Collectors.toCollection(HashSet::new));

This retains elements which satisfy the provided predicate sale -> sale.getMap().equals(mapName).

Note the above does not modify the source, if you want to modify the source then proceed with removeIf:

salesList.removeIf(sale -> !sale.getMap().equals(mapName));

This removes the elements which satisfy the provided predicate sale -> !sale.getMap().equals(mapName).

like image 154
Ousmane D. Avatar answered Oct 19 '22 23:10

Ousmane D.