Comparison of imperative and functional approach in Java -
i have methods prints name of person given list if has indicated age. method implemented using imperative , functional approach.
public static void printperson(int age) { for(person p: list) { if(p.age == age) { system.out.println(p.name) } } }
functional approach:
public static void printperson(int age) { list.stream() .filter(p -> p.age == age) .foreach(p -> system.out.println(p.name)); }
the question is, besides readability, how else can compare these 2 approaches , evaluation each of attributes. example, 1 has higher memory footprint, 1 introduces least overhead, or has higher response time. other attributes (i.e., non-functional requirements) can discussed?
in practice, doesn't matter, you'll have higher memory footprint , cpu use functional approach because lambdas relatively expensive compared imperative approach. lambdas anonymous inner classes, you're depending on jvm optimize away method calls imperative version. thing remember imperative programming looks lot assembly, tends run fast little work.
in future, operations map
parallelized, it's future-proof.
be warned, though: things lot cleaner imperatively, if focused on functional programming, might miss solution. there lot of questions on stackoverflow "how do x in guava" answer "why trying abuse guava can efficiently java's standard library in 3 lines of code?
Comments
Post a Comment