Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package fp; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Comparator; import java.util.Map; import java.util.function. * ; import java.util.List; class ExerciseNotCompletedException extends RuntimeException { public ExerciseNotCompletedException ( )
package fp;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.function.;
import java.util.List;
class ExerciseNotCompletedException extends RuntimeException
public ExerciseNotCompletedException
superExercise is not completed yet";
@link HW is an exercise class. Each method returns a functional interface and it should be implemented
using either lambda or a method reference. Every method that is not implemented yet throws
@link ExerciseNotCompletedException
TODO: remove exception and implement each method of this class using lambda or method reference
TODO: to get the most out of your learning, visit our website
record pair U u V v
public class HW
precondition: l and l have the same length
static List zipList l List l
walk through the Us and Vs
construct list of pairs
ArrayList l new ArrayList;
for int i ; i lsize; i
laddnew pairlgetilgeti;
return l;
ArrayList l new ArrayList;
for int i ; i lsize; i laddi;
return mapl i new pairlgeti lgeti;
postcondition: result has the same length as l
static List mapList l Function f
walk through the Us
use f at every stage fapply
construct list of Vs
ArrayList l new ArrayList;
for U x: l
laddfapplyx;
return l;
static List flatmapList l Function f
walk through the Us
use f at every stage fapply
construct list of Vs
ArrayList res new ArrayList;
for U x: l
List l fapplyx;
for V y: l
res.addy;
return res;
foldleftxy xy
U V Integer
fpldleftxy xy
static V foldLeftV e Iterablel, BiFunction f
walk through the Us uuun
e
use f at every stage v fapplyeu
v fapplyvu
v fapplyvu
return the last v
V v e;
for U x: l
v fapplyvx;
return v;
similar to above
but from the right
vn fune
vn funvn
return the first v
static V foldRightV e Listl, BiFunction f
walk through the Us uuun
e
use f at every stage v fapplyue
v fapplyuv
v fapplyuv
return the last v
V v e;
for int i lsize; i ; i
v fapplylgetiv;
return v;
return true if there is an element in l that satisfies p
static boolean existsIterable l Predicate p
return false;
public static List filterList l Predicate p
ArrayList l new ArrayList;
for U x: l
if ptestx
laddx;
return null;
static record PersonString name, int age, boolean teach
public static void printFilteredArrayList l Predicate f
List l filterlf;
maplPerson preturn pname; ;
precondition: l is not empty
static U minValIterable l Comparator c
write using fold. No other loops or recursion permitted.
use ccompare
return null;
Returns @link Supplier that always supply "Hello"
@return a string supplier
public static Supplier helloSupplier
throw new ExerciseNotCompletedException;
Returns a @link Predicate of string that checks if string is empty
@return a string predicate
public static Predicate isEmptyPredicate
throw new ExerciseNotCompletedException;
Return a @link Function that accepts @link String and returns that string repeate
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started