Java 8 – Calling a multi argument method from Collection.stream.map()

  • A+
Category:Languages

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()?

 


Use a lambda expression:

names.stream().map(name -> MyClass.createReport(name,designation))... 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: