0

I have some situation where I want to pass method reference of a method which have more than one arguments in forEach, I'm able to pass using single arg method which is execute(Map page) but I do not know how could I pass execute(Map page, Integer userInstanceId) in forEach, need help.

static List<Map<String,Object>> pages = new ArrayList<>();
static Map<String,Object> pageIdWithPageInstanceId = new HashMap<>();

public static void testMethodReference() {
    TestListMapLamda tt = new TestListMapLamda();
    Integer userInstanceId = 22;
    pages.stream().filter(page -> null != pageIdWithPageInstanceId.get(page.get("PageID"))).forEach( tt::execute);//this calling the single arg action which is execute(Map<String,Object> page) but not execute(Map<String,Object> page, Integer userInstanceId)       
}
public void execute(Map<String,Object> page) {
    page.put("UserInstanceID", 1111);
}

public void execute(Map<String,Object> page, Integer userInstanceId) {
    page.put("UserInstanceID", userInstanceId);
}

I can call execute(Map page, Integer userInstanceId) by following code, but I'd like to have some more compact stile like the one argument method reference .

    public static void testMethodReference2() {

    TestListMapLamda tt = new TestListMapLamda();
    Integer userInstanceId = 22;
    pages.stream().filter(page -> null != pageIdWithPageInstanceId.get(page.get("PageID"))).forEach( page -> {
        tt.execute(page, userInstanceId);
    });     

}
Assad
  • 31
  • 2
  • 3
    You can use `page -> tt.execute(page, userInstanceId)` instead of `page -> { tt.execute(page, userInstanceId); }` but you can’t use a method reference here. – Holger Apr 21 '16 at 10:44
  • 2
    Btw., you may consider using `.filter(page -> pageIdWithPageInstanceId.contains(page.get("PageID")))` instead of `.filter(page -> null != pageIdWithPageInstanceId.get(page.get("PageID")))` – Holger Apr 21 '16 at 10:45
  • 1
    @Holger Did you mean `pageIdWithPageInstanceId.containsKey(page.get("PageID").toString()))`? – fps Apr 21 '16 at 13:17
  • @Holger I was thinking that might be there were solution using method reference, but yes your solution is also more compact form thanks. – Assad Apr 21 '16 at 15:53

0 Answers0