What is j1 and j2 here?

+1 Hemlata Mahaur · November 1, 2016
What is j1 and j2 in the following statement:
  (j1, j2) -> j1.merge(j2)
Plus if they are the first two word (Max, Peter), then is it taking first, first two words and merging them. Later it takes another two words (Pamela, David), merge them and at last merge all  four together?
I'm not getting how it's working

Post a Reply


Oldest  Newest  Rating
0 Bazza Cuda · November 13, 2016
j1 and j2 represent the two StringJoiners to be merged.

0 Bazza Cuda · November 13, 2016
I'm writing this on my phone in a one inch tall edit box, so please bear with me :)

Each of the four parameters to Collector.of is a lambda expression which denotes the implementation of a Functional Interface's [one] abstract method.

This is more easily understood if you imagine that, to the compiler

  (j1, j2) -> j1.merge(j2)

... actually means something like...

private StringJoiner joinThem(StringJoiner j1, StringJoiner j2) {
  return j1.merge(j2);

So, the first parameter returns the initial empty StringJoiner, which will use the pipe character to delimit the values added to it.

The second parameter [function] is passed this empty StringJoiner and the Uppercased name1, "MAX". The function returns a new StringJoiner with name1 added to it.

The third function then merges the initial empty StringJoiner with the new one returned by function 2, using the delimiter to separate the names. 

Functions 2 and 3 are repeated for each person in persons.

Finally, the fourth static function (. toString) is passed with the final returned StringJoiner from function/parameter 3 which then returns the entire string...


Does that help ?
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator