Understand more than Iteration methods in JavaScript from a Java perspective

Tram Ho

Do you really understand all about Iteration methods like forEach() , map() , filter() , … in JavaScript? If you’ve never touched Java before, the answer will be no .

The article presents the reason why such a thing happened. Java has nothing to do with JavaScript. Ok, let’s start exploring the magic behind the above methods, from a Java perspective.

1. From the apparent simplicity of JavaScript

1.1. Iteration methods

Surely every JavaScript developer has used iteration methods like forEach() , map() , …. Here is a simple example.

To be honest, JavaScript is so simple that you might wonder how complex the code above is?

Just simply:

  • Some of the commonly used methods of JavaScript
  • The methods receive a callback function
  • Callback function is written as an arrow function

All that is it, there’s no such thing as difficult, right?

1.2. Compare with pure code

Everyone knows that instead of using some method, forEach() for example, you can use pure code to write.

Or another method is filter() .

1.3. So where is the complexity?

If you have JavaScript proficiently coded, you can code the above methods like flying without thinking much. However, that is simply because you have been familiar with and have coded many times .

When I first started learning the above methods, has anyone ever asked questions like this?

What is the function passed to forEach() ?

Is the callback function of map() different from the forEach() ?

That proves, forEach() or something else is not as simple as you might think. Understanding how to run is one thing, code is one thing, but understanding why is like that is another …

2. To the magic complexity in Java

2.1. Does Java have forEach() or something?

Java 8 added the Stream API feature, introducing the Stream concept. Basically every Stream can use the functions forEach() , …

All Collection such as List, Set, .. of java are implements Stream interface. Hence, we can easily call forEach() , … on it.

However, when using it, we cannot write like JavaScript.

2.2. How did Java do that?

As above, we know that Java cannot pass a function to another function parameter. But the hard comes the wisdom , Java has designed a refreshing, super magic and suitable for strong typed languages ​​like Java.

Specifically, java introduces the concept of Functional interface .

The above code is a built-in functional interface of java, I rewrote it as an example. Functional interface is just an interface, there is only one abstract method.

Err, so what does it have to do with JavaScript?

Since Java cannot pass a function to another function, instead of passing a callback function, java will pass an implements of Functional interface .

This is the structure of the forEach() method (a sample example).

Oh, have you seen anything related here. The code of forEach() quite similar to the JavaScript side. The c.accept(element) not the equivalent of the following JavaScript.

See, instead of passing a function directly like JavaScript, java puts that function into a functional interface, and passes the implementation of the functional interface to the function.

2.3. What is Lambda?

Pay attention to the paragraph above, I have bolded the implements of the functional interface . Functional interface is just an interface, has the code to execute, so there must be something (temporarily called X), implements functional interface with code to execute, and we will pass X to the function forEach() , …

So how to create X? Actually in java there are 4 ways:

  • Use class implements, then create object from class, pass the object to the function
  • Direct form implements always
  • Use lambda
  • Use the reference method

Method 2 is often used in the past, namely code like this.

Compared to JavaScript, the above method is too verbose.

Therefore, people use lambda instead.

Although the lambda is quite similar to the arrow function of JavaScript, there are differences:

  • Arrow function is short form of function
  • Lambda actually returns a functional interface

Hence, the lambda t -> System.out.println(t) is a functional interface.

And the lambda can automatically infer the data type. Since forEach() accepts the Consumer interface, and Consumer has only one method whose parameter is Integer (do generics), t is of type Integer .

The last is a shorthand for the lambda, which is called the reference method. Use when lambda is short as above.

3. The functional interfaces are available in java

3.1. Built in functional interfaces

Java has built in many functional interfaces, each corresponding to a type of method within it (the method depends on the parameter and return type):

  • Consumer<T> { void accept(T t); } : This guy only knows how to eat (has 1 parameter) but doesn’t return anything (no return)
  • Supplier<T> { T get(); } : Only return, do not take any parameters
  • Function<T, R> { R apply(T t); } : Take 1 parameter, return a value
  • Predicate<T> { boolean test(T t); } : The judge is here, takes a param and returns true and false

Above are 4 basic functional interfaces. There are also functional interfaces BiABC format instead of taking 1 parameter, it will take 2 parameters.

So what to do with forEach() , map() , … this and that?

3.2. The main point of the article

At this point, you will understand the relationship between Java and JavaScript.

Each iteration method will receive a corresponding functional interface.

And this is the main point of this article.

In JavaScript does not show that clearly, due to the weakly typed nature of the language also applies to the function. However, when methods like forEach() entered Java, it made a difference:

  • forEach() used with the Consumer interface
  • map() used with the Function interface
  • filter() used with the Predicate interface
  • find() , findValue() used with the Predicate interface
  • BiFunction reduce() use with BiFunction interface (because there are 2 parameters, and return a value)

The reason is easy to infer from the context. This in Java can be extended to JavaScript as well.

For example, with JavaScript when using filter() , the following code returns incorrect results.

Since filter() not for Consumer , it has to be Predicate , get e and return a boolean . Fix like this.

Okay the article is quite long, I just stopped here. Thank you for taking the time to read until now, I am grateful for that. Hopefully JavaScript developers will understand more about iteration methods, and use them more accurately.

Happy coding

Share the news now

Source : Viblo