Java 流减少

2022-09-04 22:17:32

我有以下示例数据集,我想根据方向的值使用Java流API进行转换/减少

Direction    int[]
IN           1, 2
OUT          3, 4
OUT          5, 6, 7
IN           8
IN           9
IN           10, 11
OUT          12, 13
IN           14

Direction    int[]
IN           1, 2, 
OUT          3, 4, 5, 6, 7
IN           8, 9, 10, 11
OUT          12, 13
IN           14

到目前为止我编写的代码

enum Direction { IN, OUT }

class Tuple {
  Direction direction;
  int[] data;

  public Tuple merge(Tuple t) {
      return new Tuple(direction, concat(getData(), t.getData()));
  }
}

private static int[] concat(int[] first, int[] second) {
    int[] result = Arrays.copyOf(first, first.length + second.length);
    System.arraycopy(second, 0, result, first.length, second.length);
    return result;
}

List<Tuple> reduce = tupleStream.reduce(new ArrayList<>(), WDParser::add, WDParser::combine);

private static List<Tuple> combine(List<Tuple> list1, List<Tuple> list2) {
    System.out.println("combine");
    list1.addAll(list2);
    return list1;
}

private static List<Tuple> add(List<Tuple> list, Tuple t) {
    System.out.println("add");
    if (list.size() == 0) {
        list.add(t);
    } else if (list.size() > 0) {
        int lastIndex = list.size() - 1;
        Tuple last = list.get(lastIndex);
        if (last.getDirection() == t.getDirection())
            list.set(lastIndex, last.merge(t));
        else
            list.add(t);
    }

    return list;
}

我相信有一个更好,更简单的替代方案来实现同样的目标。

我发现的Java流API reduce/combine的在线示例和博客仅使用Integer::sum函数。希望为更复杂的案例场景建立起来。


答案 1

我认为您的解决方案已经很不错了,特别是因为与收集到共享的外部容器中相比,使用缩减可以轻松实现并行性。但它更容易使用,而不是像Holger指出的那样。此外,累加器中的条件可以稍微简化一下,并且您忘记合并合并器中的最后一个和第一个元素:collectreduce

List<Tuple> reduce = tupleStream.collect(ArrayList::new, WDParser::add, WDParser::combine);

private static List<Tuple> combine(List<Tuple> list1, List<Tuple> list2)
{
    if (!list2.isEmpty())
    {
        add(list1, list2.remove(0)); // merge lists in the middle if necessary
        list1.addAll(list2);         // add all the rest
    }
    return list1;
}

private static List<Tuple> add(List<Tuple> list, Tuple t)
{
    int lastIndex = list.size() - 1;
    if (list.isEmpty() || list.get(lastIndex).getDirection() != t.getDirection())
    {
        list.add(t);
    }
    else
    {
        list.set(lastIndex, list.get(lastIndex).merge(t));
    }
    return list;
}

而不是使用索引来访问您甚至可以使用的第一个/最后一个元素和方法。LinkedListadd/removeFirst/Last()


答案 2

怎么样。首先定义一个较小的帮助器方法:

private static Tuple mergeTwo(Tuple left, Tuple right) {
    int[] leftArray = left.getData();
    int[] rightArray = right.getData();
    int[] result = new int[leftArray.length + rightArray.length];
    System.arraycopy(leftArray, 0, result, 0, leftArray.length);
    System.arraycopy(rightArray, 0, result, leftArray.length, rightArray.length);
    return new Tuple(left.getDirection(), result);
}

这接近你的我猜,但一个。基本上是一种将两个(s)合并在一起的方法。concat/mergeTuple

而一个辅助方法产生所需的,你可以把它放到一个实用程序中,这样它就可以被重新使用:Collector

private static Collector<Tuple, ?, List<Tuple>> mergedTuplesCollector() {
    class Acc {

        ArrayDeque<Tuple> deque = new ArrayDeque<>();

        void add(Tuple elem) {
            Tuple head = deque.peek();
            if (head == null || head.getDirection() != elem.getDirection()) {
                deque.offerFirst(elem);
            } else {
                deque.offerFirst(mergeTwo(deque.poll(), elem));
            }
        }

        Acc merge(Acc right) {

            Tuple lastLeft = deque.peekLast();
            Tuple firstRight = right.deque.peekFirst();

            if (lastLeft.getDirection() == firstRight.getDirection()) {
                deque.offerLast(mergeTwo(deque.pollLast(), right.deque.pollFirst()));
            } else {
                deque.addAll(right.deque);
            }

            return this;
        }

        public List<Tuple> finisher() {
            return new ArrayList<>(deque);
        }

    }
    return Collector.of(Acc::new, Acc::add, Acc::merge, Acc::finisher);
}

例如,用法是:

List<Tuple> merged = tuples.stream()
            .parallel()
            .collect(mergedTuplesCollector());