Proper syntax for this lambda expression

Saad :

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.

Eugene :

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;
  })

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=93232&siteId=1