@Override
    protected void sideEffect(final Traverser.Admin<S> traverser) {
        final Map<K, V> map = new HashMap<>(1);
        this.valueTraversal.reset();
        this.valueTraversal.addStart(traverser);

        TraversalUtil.produce(traverser, this.keyTraversal).ifProductive(p -> {
            if (null == this.barrierStep) {
                if (this.valueTraversal.hasNext()) {
                    map.put((K) p, (V) this.valueTraversal.next());
                }
            } else if (this.barrierStep.hasNextBarrier()) {
                map.put((K) p, (V) this.barrierStep.nextBarrier());
            } else if (this.barrierStep instanceof FilteringBarrier) {
                final int barrierStepIndex = TraversalHelper.stepIndex((Step) barrierStep, valueTraversal) + 1;
                if (valueTraversal.getSteps().stream().skip(barrierStepIndex).anyMatch(
                        step -> step instanceof SupplyingBarrierStep || step instanceof ReducingBarrierStep)) {
                    map.put((K) p, (V) this.barrierStep.getEmptyBarrier());
                }
            }
        });

        if (!map.isEmpty())
            this.getTraversal().getSideEffects().add(this.sideEffectKey, map);
    }
@author Marko A. Rodriguez (http://markorodriguez.com)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/sideEffect/GroupSideEffectStep.java