Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can use functional operations

Tags:

I'm using the for-each construct in Java as follows:

public int getNumRStations() {     int numRoutes = 0;      for (ArrayList<Route> route : routes) {         numRoutes += route.size();     }      return numRoutes; } 

NetBeans shows a warning/notice that says "Can use functional operations". Upon automatically resolving it, the newly generated code shows this:

public int getNumRStations() {     int numRoutes = 0;      numRoutes = routes.stream().map((route) -> route.size()).reduce(numRoutes, Integer::sum);      return numRoutes; } 
  1. Why is NetBeans warning me of this? I know I'm not supposed to blindly trust IDEs, so that's why I'm asking.
  2. What is that new line supposed to do? I haven't seen anything like it, in real life or in class.
like image 261
rink.attendant.6 Avatar asked Aug 14 '14 03:08

rink.attendant.6


People also ask

What is the use of functional interface?

Functional interfaces are interfaces that ensure that they include precisely only one abstract method. Functional interfaces are used and executed by representing the interface with an annotation called @FunctionalInterface. As described earlier, functional interfaces can contain only one abstract method.

Can Java be used for functional programming?

Java is a functional style language and the language like Haskell is a purely functional programming language. Let's understand a few concepts in functional programming: Higher-order functions: In functional programming, functions are to be considered as first-class citizens.

Why do we use functional interface in Java?

Objects are the base of java programming language and we can never have a function without an Object, that's why Java language provide support for using lambda expressions only with functional interfaces.

What is Java functional interface?

A functional interface in Java is an interface that contains only a single abstract (unimplemented) method. A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.


1 Answers

That looks like NetBeans suggesting refactoring your sum operation as a Java 8 "lambda" or functional programming operation using the map and reduce functions from the Stream interface. You must be using a Java 8 JDK with NetBeans.

Breaking down what it's doing:

  • the "map()" function is performing a count of all of your route sizes in your routes array list,

  • the "reduce()" function is then performing a sum of those individual sizes to get the final result for the total number of routes.

The map() and reduce() functions are documented in the Java 8 documentation for the Stream interface available at this link

This answer addresses "what it is" but doesn't address "why it's better". I will admit to still learning about these constructs myself.

like image 145
paisanco Avatar answered Nov 11 '22 07:11

paisanco