Dynamic filtering conditions in Lambda

Daniel Sachse mail at wombatsoftware.de
Fri Oct 11 15:25:25 PDT 2013


Hey guys,

I having the following working Java 8 code:

List<String> names = Arrays.asList("Ben", "Brad", "Bill", "Kim", "Kara",
"Sam", "Sara", "Jil", "Brenda");

List<String> result = names.stream().filter(name -> name.startsWith("K"))

                .filter(name -> name.length() == 3)
                .collect(toList());

============

Now I change it to use multiple possible first letters and lenghts:

List<String> result = names.stream().filter(name -> name.startsWith("K") ||
name.startsWith("B"))

                .filter(name -> name.length() == 3 || name.length() == 4)
                .collect(toList());


============

Now I want to rewrite this and use a dynamic list of starting letters and a
dynamic list of length attributes, but I totally got stuck on how to
implement this :

List<String> names = Arrays.asList("Ben", "Brad", "Bill", "Kim", "Kara",
"Sam", "Sara", "Jil", "Brenda");

*List<String> *startsWith* = Arrays.asList("B", "K", "S");*
*List<Integer> *length* = Arrays.asList(2, 3, 4);*

List<String> result = names.stream().
// Filters missing

                .collect(toList());


============

I would be really glad if you could provide me with a solution how this
could be implemented using Lambdas :)


Thanks in advance!

Daniel

--


Wombat Software Technologies UG (haftungsbeschränkt)
Im MediaPark 5
D-50670 Köln

Geschäftsführer: Daniel Sachse, Jacob Pawlik
Unternehmenssitz: Köln
Handelsregister beim Amtsgericht: Köln
Handelsregister-Nummer: HRB 79316

Web: http://www.wombatsoftware.de
Email: mail at wombatsoftware.de
Tel.: 0221/16905638
Mobil: 01578/4922886


More information about the lambda-dev mailing list