索引化<流>数据 Java

huangapple go评论72阅读模式
英文:

indexing <Stream> data Java

问题

需要一些关于在Java中索引流数据的帮助。背景是我们需要手动为嵌入到其他文档中的文档设置索引(简而言之,此方法的输出需要是一个流)。

return Stream.concat(firstStream, secondStream) // 这些需要被索引
      .sorted(// 使用比较器排序)
      .forEach? .map? // 该类具有带有getter和setter的索引字段,所以我认为需要执行 `setIndex(i)`,但不确定从哪里获取'i'

任何建议将不胜感激!

英文:

need some help with indexing Stream data in Java. The context is that we need to manually set index for document that is embedded to other document (tldr; the output needs to be Stream in this method)

return Stream.concat(firstStream, secondStream) &lt;- these need to be indexed
      .sorted(// sorted using Comparator)
      .forEach? .map? // the class has index field with getter and setter so I think need to do `setIndex(i)` but wasnt sure where to get &#39;i&#39;

Any advice would be greatly appreciated!

答案1

得分: 3

如果您可以自己从列表构建流,请使用索引的 IntStream,而不是对象的 Stream

IntStream.range(0, firstList.size()).forEach(i -> firstList.get(i).setIndex(i));
int offsetForSecondList = firstList.size();
IntStream.range(0, secondList.size())
        .forEach(i -> secondList.get(i).setIndex(offsetForSecondList + i));

我没有尝试编译代码,所以请原谅可能的拼写错误。

否则,您的 AtomicReference 方法也可以工作。

英文:

If you can construct your streams yourself from lists, use IntStream of indices rather than Stream of objects.

IntStream.range(0, firstList.size()).forEach(i -&gt; firstList.get(i).setIndex(i));
int offsetForSecondList = firstList.size();
IntStream.range(0, secondList.size())
        .forEach(i -&gt; secondList.get(i).setIndex(offsetForSecondList + i));

I have not tried to compile the code, so forgive any typo.

Otherwise your AtomicReference approach works too.

答案2

得分: 1

假设您有一个名为MyObject的类:

class MyObject{
    int index;
    String name;
    //getters,setters,cons, toString...
}

类似下面的内容可能是一个起始点:

public static Stream<MyObject> fooBar(){
    //仅作示例,为了使流能够连接
    List<MyObject> first = List.of(new MyObject("foo"),new MyObject("foo"),new MyObject("foo"));
    List<MyObject> second = List.of(new MyObject("bar"),new MyObject("bar"),new MyObject("bar"));
    
    AtomicInteger ai = new AtomicInteger(0);
    
    return Stream.concat(first.stream(), second.stream())
            .peek(myo -> myo.setIndex(ai.getAndIncrement()));
}
英文:

Assuming you have a class MyObject:

class MyObject{
    int index;
    String name;
    //getters,setters,cons, toString...
}

Something like below may be a starting point:

public static Stream&lt;MyObject&gt; fooBar(){
    //just for example, inorder to get the streams to be concatnated
    List&lt;MyObject&gt; first = List.of(new MyObject(&quot;foo&quot;),new MyObject(&quot;foo&quot;),new MyObject(&quot;foo&quot;));
    List&lt;MyObject&gt; second = List.of(new MyObject(&quot;bar&quot;),new MyObject(&quot;bar&quot;),new MyObject(&quot;bar&quot;));
    
    AtomicInteger ai = new AtomicInteger(0);
    
    return Stream.concat(first.stream(), second.stream())
            .peek(myo -&gt; myo.setIndex(ai.getAndIncrement()));
}

huangapple
  • 本文由 发表于 2020年5月5日 00:09:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定