10

I've been using the java 8 Streams for a while. I came across a situation where I need to stream through a List and pass each element to a static method along with another argument. Is it possible in java 8?

........
String designation = "Engineer";
List<String> names = new ArrayList<>();
names.add("ABC");
names.add("DEF");
names.add("GHI");
names.stream().map(MyClass::createReport);
..........

class MyClass {
    public static void createReport(String name, String designation) {
       System.out.println(name+"\t"+designation);
    }
}

How can I pass the designation String via stream().map()?

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Spartan
  • 339
  • 1
  • 3
  • 14

4 Answers4

11

Use a lambda expression:

names.stream().map(name -> MyClass.createReport(name,designation))...
Eran
  • 387,369
  • 54
  • 702
  • 768
2

You could write a curried version of the method createReport.

Curried createReport

We need to swap the order of the arguments because the designation for each name is the same. Additionly we just need to call the not curried method.

Function<String, Consumer<String>> createReportCurry = (designation) -> (name) -> {
    createReport(name, designation);
};

In Action

names.stream().forEach(createReportCurry.apply(designation))
Roman
  • 4,922
  • 3
  • 22
  • 31
0

You could also use the following as an alternative:

IntStream.range(0, names.size())
         .forEach(i -> MyClass.createReport(names.get(i), names.get(i)));
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 3
    why the intstream? – Felk Nov 29 '18 at 14:22
  • designation was actually set/known from outside, so you may want to keep that and yes... why intstream? Erans answer is probably already simple enough... – Roland Nov 29 '18 at 14:23
  • @Felk : How else would I get the index? – Nicholas K Nov 29 '18 at 14:24
  • 1
    you don't need to... you can just use the stream on the list itself and use its contents (i.e. the `name`)... just check Erans answer... – Roland Nov 29 '18 at 14:25
  • Well, its another alternative. – Nicholas K Nov 29 '18 at 14:27
  • that's true... but still, you may want to use `designation` as the second parameter, as that's also what the OP has used ;-) – Roland Nov 29 '18 at 14:56
  • One of the benefits of using streams or `Collection.forEach()` is that they provide internal iteration through functional APIs. When using an `IntStream` to generate list indexes, you are back to external iteration and thus more or less the same as `for(int i; i < names.size(); i++)`. See also [this question](https://stackoverflow.com/q/224648/525036) and [this video from Venkat Subramaniam](https://www.youtube.com/watch?v=FnL8GB_d4bs) – Didier L Nov 29 '18 at 17:47
0

The above answer are very good but let's try to explain why you couldn't run your code in the first place:

Your code:

names.stream().map(MyClass::createReport);names.stream().map(MyClass::createReport);

What the JVM undertands

names.stream().map(MyClass::createReport);names.stream().map(name -> MyClass.createReport(name));

Solutions use a basic lamba as @Eran or a Function as @Roman suggested.