无法将IntStream转换为某些对象流 [英] Cannot convert IntStream to some Object Stream

查看:127
本文介绍了无法将IntStream转换为某些对象流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 IntStream 来实例化对象流:

I'm trying to use an IntStream to instantiate a stream of objects:

Stream<MyObject> myObjects = 
       IntStream
        .range(0, count)
        .map(id -> new MyObject(id));

但它说无法转换 MyObject int

推荐答案

IntStream class的 map 方法 int s映射到更多 int s,带有 IntUnaryOperator int int ),而不是对象。

The IntStream class's map method maps ints to more ints, with a IntUnaryOperator (int to int), not to objects.

通常,所有流' map 方法都会映射类型流到自身, mapToXyz 映射到不同的类型。

Generally, all streams' map method maps the type of the stream to itself, and mapToXyz maps to a different type.

尝试 mapToObj 方法,它取一个 IntFunction int 到对象)相反。

Try the mapToObj method instead, which takes an IntFunction (int to object) instead.

.mapToObj(id -> new MyObject(id));

这篇关于无法将IntStream转换为某些对象流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆