简体   繁体   中英

Proper syntax for this lambda expression

I have a list A. And from that list, i want to create a new list B by using one field of list A for construction of objects in list B. However i am unable to get the syntax right. Currently i have

List<B> listB = listA.stream().map(id -> {
    ObjectB b = Mockito.mock(ObjectB.class);
    when(b.getId()).thenReturn(id.toString());
    when(b.getNumericId()).thenReturn(id); 
}).collect(Collectors.toList());

However i am getting syntax error on map which i am unable to understand.

If you have used {} for the lambda creation, you are supposed to use return also, thus:

  List<B> listB = listA.stream().map(id -> {
         ObjectB b = Mockito.mock(ObjectB.class);
         when(b.getId()).thenReturn(id.toString());
         when(b.getNumericId()).thenReturn(id); 
         return b;
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM