我有一个列表A。从列表中,我想通过使用列表A的一个字段来构造列表B中的对象来创建新列表B。但是,我无法正确使用语法。 目前我有
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());
但是我在地图上遇到语法错误,我无法理解。
答案 0 :(得分:4)
如果您已使用{}
用于创建lambda,则还应该使用return
,因此:
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;
})