随着Collection一切都清楚了,但是怎么样以下几点:
有object一种count()方法和getPart(int i)方法.因此,提取所有对象会产生以下样板代码:
List<Part> result = new ArrayList<Part>();
for (int i = 0, i < object.count(), i++) {
result.add(object.getPart(i));
}
return result.stream();
Run Code Online (Sandbox Code Playgroud)
是否有任何标准的方法来传递2个生产者:() -> object.count()并(int i) -> object.getPart(i)创建一个流?像这样:
SomeUtil.stream(object::count, object::getPart);
Run Code Online (Sandbox Code Playgroud) Streams API中缺少的一个功能是"分区依据"转换,例如Clojure中定义的.假设我想重现Hibernate的fetch join:我想发出一个SQL SELECT语句来从结果中接收这种对象:
class Family {
String surname;
List<String> members;
}
Run Code Online (Sandbox Code Playgroud)
我发出:
SELECT f.name, m.name
FROM Family f JOIN Member m on m.family_id = f.id
ORDER BY f.name
Run Code Online (Sandbox Code Playgroud)
我检索一个平坦的(f.name, m.name)记录流.现在我需要将其转换为Family对象流,并在其中包含其成员列表.假设我已经有了Stream<ResultRow>; 现在我需要将其转换为a Stream<List<ResultRow>>然后使用映射转换对其进行操作,将其转换为a Stream<Family>.
转换的语义如下:List只要提供的鉴别器函数保持返回相同的值,就保持将流收集到for中; 一旦值改变,发出List作为输出流的元素并开始收集新的List.
我希望能够编写这种代码(我已经有了这个resultStream方法):
Stream<ResultRow> dbStream = resultStream(queryBuilder.createQuery(
"SELECT f.name, m.name"
+ " FROM Family f JOIN Member m on m.family_id = f.id"
+ " …Run Code Online (Sandbox Code Playgroud) 我正在尝试扩展Java 8的Stream实现.
我有这个界面:
public interface StreamStuff<T> extends Stream<T> {
Stream<T> delegate();
default Stream<T> biggerThanFour() {
return delegate().filter(i -> ((Double)i > 4));
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主要方法:
int arr [] = {1,2,3,4,5,6};
Object array [] = ((StreamStuff)Arrays
.stream(arr))
.biggerThanFour()
.toArray();
Run Code Online (Sandbox Code Playgroud)
我正在尝试将Stream转换为我的接口StreamStuff,并使用我的方法.
我收到以下错误:
线程"main"中的异常java.lang.ClassCastException:java.util.stream.IntPipeline $ Head无法强制转换为StreamStuff
当我这样做时,我得到同样的错误:
StreamStuff ss = (StreamStuff)Arrays.stream(arr);
我想知道这种事情是否可能,如果是这样,我该如何实现这一目标?作为参考,我有点使用这篇文章作为指导.
你好:我想知道如何编写一个异步表迭代器。假设输入表由很多行组成,当接收到该表时,它是序列化的格式。当接收到表时,迭代器被调用以一行一行地检索。
它通过以下方式执行读取和反序列化: 1) 它首先读取关于行大小的整数并将其反序列化。2) 然后它读取并反序列化该行的内容,其中,a。时间戳首先通过调用 in.readint(), b 准备好。然后读取和反序列化该行的每个键,c。然后读取和反序列化有关非键列的位图字符串。d. 然后调用 in.readint() 读取并反序列化表示非键列数的整数,然后读取并反序列化每个非键列。3) 最后它读取并反序列化文件结束标记,该标记指示是否到达文件末尾。
最后它返回反序列化的行。
这是代码
enter code here
public Row next() {
/* It first reads the integer about the size of the row and
deserialize it. */
int size = in.readInt();
/*Then it reads and deserialize the contents of the row*/
Row row = Row.deserialize(descriptor, in);
/*Finally it reads and deserializes the file end marker, which
indicates if the end of the file is reached.*/
int signal = in.readInt();
if …Run Code Online (Sandbox Code Playgroud)