1

I build a small example to test the java 8. Having a list of Strings:

List<String> list = Arrays.asList(new String[] { "Sheep", "Ship","Skeleton" });

The following function:

list.stream().forEach(name -> list.forEach(name2->System.out.println("Name2:"+name2+" name:" + name + "=>" + name2.compareTo(name))));

returns the comparison between name2 and name1.

I am trying to test and I don't know if it is feasible this scenario: Having the same list and an independent String variable called lastFoundWord can I assign with a "lambda way" the variable just right after the comparison as it happens with the previous line ?

Note:I see that forEach can not accept a list of Consumers, and Consumer building is very strict. I am not sure about the "Function" interface. All my thought directs me to iterate twice the same information.

Is there perhaps a forEach function that accepts a list of Consumers and I missed it ?

hephestos
  • 434
  • 1
  • 6
  • 19

1 Answers1

1

Using consumers from a list is something I try to figure.

Although for my current example, I found a solution using braces. So the code turned to that:

list.stream().forEach(name -> list.forEach(name2 -> {
    System.out.println("Name2:" + name2 + " name:" + name + "=>" + name2.compareTo(name));
    lastName = name2;
}));
John Saunders
  • 160,644
  • 26
  • 247
  • 397
hephestos
  • 434
  • 1
  • 6
  • 19