Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I filter map entries based on set of entries

I'm using google guava 12 and have a map:

Map<OccupancyType, BigDecimal> roomPrice;

I have a Set:

Set<OccupancyType> policy;

How can I filter entries in the roomPrice map based on policy and return the filtered map ?

filteredMap needs to have all the values from policy. In case, roomPrice map doesnt have an entry from policy, I'd like to input default value instead.

like image 205
brainydexter Avatar asked Jun 27 '12 07:06

brainydexter


People also ask

How do you filter maps?

Step 1 Go to Add/Manage Map -> Edit Page. Step 2 Assign those Locations using Choose Locations section, which you want to display on a google maps. Step 3 Scroll down to Custom Filters and insert placeholders like {country},{state},{city}. Insert Filter text according to it Filter placeholder.

How do you filter a map based on value in Java 8?

filter() A more modern way to filter maps would be leveraging the Stream API from Java 8, which makes this process much more readable. The filter() method of the Stream class, as the name suggests, filters any Collection based on a given condition.

Can we use map and filter together in Java?

All you need is a mapping function to convert one object to another, and the map() function will do the transformation for you. It is also an intermediate stream operation which means you can call other Stream methods like a filter or collect on this to create a chain of transformation.

Can we use filter and map together in Java 8?

Once we have the Stream of Integer, we can apply maths to find out even numbers. We passed that condition to the filter method. If we needed to filter on String, like select all string which has length > 2 , then we would have called filter before map. That's all about how to use map and filter in Java 8.


2 Answers

Since you have a Set of keys you should use Maps.filterkeys(), also Guava provides a pretty good set of predicates that you can use out of the box. In your case something like Predicates.in() should work.

So basically you end up with:

Map<OccupancyType, BigDecimal> filteredMap
    = Maps.filterKeys(roomPrice, Predicates.in(policy));

Hope it helps.

like image 171
Francisco Paulo Avatar answered Oct 25 '22 02:10

Francisco Paulo


  • Override and implement equals and hashcode in OccupancyType.
  • Loop through roomPrice's keyset and collect the elements contained in the filter.

Something like this:

Map<OccupancyType, BigDecimal> filteredPrices = new HashMap<OccupancyType, BigDecimal>();
for(OccupancyType key : roomPrice.keySet()) {
    if(policy.contains(key) {
        filteredPrices.put(key, roomPrice.get(key));
    }
}

Update

Ok after reading up a bit on Google Guava, you should be able to do something like:

Predicate<OccupancyType> priceFilter = new Predicate<OccupancyType>() {
    public boolean apply(OccupancyType i) {
        return policy.contains(i);
    }
};

and then

return Maps.filterValues(roomPrice, priceFlter);

should do the trick.

like image 20
Jeshurun Avatar answered Oct 25 '22 04:10

Jeshurun