小编bob*_*123的帖子

如何映射到多个元素并收集

final List<Toy> toys = Arrays.asList("new Toy(1)", "new Toy(2)"); 

final List<Item> itemList = toys.stream()
   .map(toy -> {
        return Item.from(toy); //Creates Item type
   }).collect(Collectors.toList);
Run Code Online (Sandbox Code Playgroud)

上面的代码可以正常工作,并将从玩具列表中生成项目列表。

我想做的是这样的:

final List<Item> itemList = toys.stream()
   .map(toy -> {
        Item item1 = Item.from(toy);
        Item item2 = Item.fromOther(toy);

        List<Item> newItems = Arrays.asList(item1, item2);
        return newItems;
   }).collect(Collectors.toList);
Run Code Online (Sandbox Code Playgroud)

或者

final List<Item> itemList = toys.stream()
   .map(toy -> {
        return Item item1 = Item.from(toy); 
        return Item item2 = Item.fromOther(toy); //Two returns don't make sense but just want to illustrate the idea.       
   }).collect(Collectors.toList); …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

2
推荐指数
1
解决办法
4439
查看次数

如何检查列表中的所有元素是否为整数

如果我有一个列表,如:

List = [12,6,3,5,1.2,5.5] 
Run Code Online (Sandbox Code Playgroud)

有没有办法可以检查所有数字是否都是整数?我试过类似的东西

def isWhole(d): 
if (d%1 == 0 ) : for z in List return true.
Run Code Online (Sandbox Code Playgroud)

这显然是非常错误的.我能做什么?

python list python-3.x

1
推荐指数
1
解决办法
1708
查看次数

标签 统计

java ×1

java-8 ×1

java-stream ×1

list ×1

python ×1

python-3.x ×1