Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the java.util.function.Predicate interface, write a static generic method List filter ( List values, Predicate

Using the java.util.function.Predicate interface, write a static generic method

   List filter ( List values, Predicate p )

that returns a list of all values for which the predicate returns true. Demonstrate how to use this method by getting a list of all strings with a length of greater than ten from a given list of strings. Use a lambda expression.

Write a static generic method

   List map( List values, Function f )

that returns a list of the values returned by the function when called with arguments in the values list.

For Example:

        ArrayList list1 = new ArrayList<>(Arrays.asList(new String[] {"Larry", "Curly", "Moe", "Shemp", "Curly Joe"}) );        System.out.printf("filter(%s, value -> value.length() == 3) returns %s", list1 ,filter( list1, value -> value.length() == 3 ));        ArrayList list2 = new ArrayList<>(Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8}) );        System.out.printf("filter(%s, value -> value %% 2 != 0 ) returns %s", list2,  filter( list2, value -> value % 2 != 0 ));        ArrayList list3 = new ArrayList<>(Arrays.asList(new String[] {"Huey", "Dewey", "Louie"}) );        System.out.printf("map(%s, Week9Program::reverseString) returns %s", list1 ,map( list3, Week9Program::reverseString ));        ArrayList list4 = new ArrayList<>(Arrays.asList(new String[] {"bob", "carol", "ted", "alice"}) );        System.out.printf("map(%s, value -> value + "@mtu.edu" ) returns %s", list4 ,map( list4, value -> value + "@mtu.edu" ));        ArrayList list5 = new ArrayList<>(Arrays.asList(new Integer[] {1, 2, 3, 4, 5, 6, 7, 8}) );        System.out.printf("map(%s, value -> Math.pow(2, value) ) returns %s", list5,  map( list5, value -> Math.pow(2, value) ));        

Produces the following output:

filter([Larry, Curly, Moe, Shemp, Curly Joe], value -> value.length() == 3) returns [Moe]filter([1, 2, 3, 4, 5, 6, 7, 8], value -> value % 2 != 0 ) returns [1, 3, 5, 7]map([Larry, Curly, Moe, Shemp, Curly Joe], Week9Program::reverseString) returns [yeuH, yeweD, eiuoL]map([bob, carol, ted, alice], value -> value + "@mtu.edu" ) returns [bob@mtu.edu, carol@mtu.edu, ted@mtu.edu, alice@mtu.edu]map([1, 2, 3, 4, 5, 6, 7, 8], value -> Math.pow(2, value) ) returns [2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0]  

PreviousNext

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intermediate Accounting

Authors: Donald E. Kieso, Jerry J. Weygandt, And Terry D. Warfield

13th Edition

9780470374948, 470423684, 470374942, 978-0470423684

More Books

Students also viewed these Accounting questions

Question

Discuss therapeutic applications of motivational interviewing.

Answered: 1 week ago