簡體   English   中英

此lambda表達式的正確語法

[英]Proper syntax for this lambda expression

我有一個列表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());

但是我在地圖上遇到語法錯誤,我無法理解。

如果你使用{}進行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;
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM