English 中文(简体)
将第一点内容从同一精简处添加到精简
原标题:Add the first element to the Stream from the same Stream

我的JsonNode精简了,我想转而精简;String[]>与CSV一样,第一行是头盔,然后我们有价值。 预计相同。 为实现这一目标,目前使用的是<代码>Stream.concat,但第一行将永远为[,因为只有在我们开始处理价值流之后,我才提取头盔。

    GoogleAdsGetValuesResponse getValuesResponse = client.getValues(getValuesRequest);
    AtomicLong consumedRows = new AtomicLong();
    List<String> headers = new ArrayList<>();
    Stream<String[]> valueStream =
        getValuesResponse
            .getValuesStream()
            .limit(request.getLimit())
            .map(
                rowNode -> {
                  if (consumedRows.get() == 0) {
                    extractAllKeys(rowNode, "", headers);
                    headers.removeIf(header -> !header.contains("."));
                  }
                  String[] row =
                      headers.stream()
                          .map(header -> rowNode.at(getJsonPointerString(header)).asText())
                          .toArray(String[]::new);
                  consumedRows.getAndIncrement();
                  return row;
                });

    Stream<String[]> headerStream = Stream.of(headers.toArray(String[]::new), new String[] {});
    Stream<String[]> dataStream = Stream.concat(headerStream, valueStream);
    return TextDataUtils.sample(dataStream, Collections.emptyMap(), request.getLimit(), false);

我如何做到这一点? 将第一点内容从同一精简处添加到精简处。 赞赏任何建议。 我确实赞赏你能够提供的任何帮助。

问题回答

Java不允许你改变收集工作,而搁置起来,因此你想要做的事情是不可能的。

我建议首先将头盔处理到<条码>第(1)条上,然后着手压缩其余处理项目使用<条码>skip(1)

大约:

Stream<String> headerStream = newResponseStream.limit(1).getHeaders()
Stream<String[]> valueStream = newResponseStream.skip(1).getRows()

Stream<String[]> dataStream = Stream.concat(headerStream, valueStream);
return TextDataUtils.sample(dataStream, Collections.emptyMap(), request.getLimit(), false);

请注意,您不能再重复起始反应。 如果根本的溪流恰当地 f取,这仍将是有效的。

As a side note, I would avoid writing map functions with side effects. It makes the code more difficult to reason about.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签