1

I have Map<String,List<Object>> and for each key i need to filter the List<Object>. When performing the filter, I need to remove items from the List<Object> .

I can iterate over the map and remove the elements that I want however, I also need to keep track of the elements that I have removed. I want the filter function to be simple and only filter, in which case you would always get a Map<String,List<Object>> with the filtered values. The problem is, how can I know which records have been removed?.

I can write another function to return the items which are discarded but this would mean that I would iterate twice over the same data structure. Or I can have variables inside the filter which would store the values (and then I can retrieve) but this looks messy. (i could also use the collectionUtils disjunction function - another option)

Are there other ways/design patterns that i can use for implementation?

Rory Lester
  • 2,858
  • 11
  • 49
  • 66
  • 1
    Something like an audit trail. Do you have API or sample code around which to build an example? – Brett Walker May 04 '15 at 10:25
  • All I have at present is a map iteration, if match criteria keep array list entry, else remove. Returns the filtered map. I can either iterate over the map again or use the apache commons api to give the disjunction (which returns elements which were removed). I wouldent want to use an api for the filtering, would like to implement this myself – Rory Lester May 04 '15 at 10:31

1 Answers1

0

Take a look at this post
1. Create an interface (or a set) to indicate which values to keep.
2. Create the filter function to store the kept values . (you can also store the discarded ones)

Community
  • 1
  • 1