Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 stream.map on same stream out different mapping functions?

Can somebody please help me in optimising the code below. I don't want to stream on same list 3 times. I have to iterate on same list and apply different mapping functions. Can somebody suggest any better solution for doing this-

List<Dummy> dummy = getDummyData(); //Assume we are getting data from some source
List<NewDummy> newDummyList = dummy.stream().map(eachDummy -> mapper.map(eachDummy, NewDummy.class)).collect(Collectors.toList());

if(someCondition) {
  final BigDecimal amount1 = dummy.stream().filter(eachDummy -> Optional.ofNullable(eachDummy.getAmount1()).isPresent())
                                  .map(Dummy::getAmount1).reduce(BigDecimal.ZERO, BigDecimal::add);
  final BigDecimal amount2 = dummy.stream().filter(eachDummy -> Optional.ofNullable(eachDummy.getAmount2()).isPresent())
                                  .map(Dummy::getAmount2).reduce(BigDecimal.ZERO, BigDecimal::add);

  return new DummyObject(newDummyList, amount1, amount2);
} else {
    return new DummyObject(newDummyList);
}
like image 557
dubpp Avatar asked Dec 23 '22 04:12

dubpp


1 Answers

This seems like the ideal use case for a custom collector. But before that, I think you can simplify the sums of the amounts as follows:

BigDecimal amount1 = dummy.stream()
    .map(Dummy::getAmount1)
    .filter(Objects::nonNull)
    .reduce(BigDecimal::add).orElse(BigDecimal.ZERO);

Now, the custom collector. You could accumulate instances of Dummy into an instance of a dedicated local class inside a static utility method:

static Collector<Dummy, ?, DummyObject> toDummyObject(
        Function<Dummy, NewDummy> mapper, 
        boolean someCondition) {

    class Accumulator {
        List<NewDummy> newDummyList = new ArrayList<>();
        BigDecimal amount1 = BigDecimal.ZERO;
        BigDecimal amount2 = BigDecimal.ZERO;

        public void add(Dummy dummy) {
            newDummyList.add(mapper.apply(dummy));
        }

        public void addAndSum(Dummy dummy) {
            if (dummy.getAmount1() != null) amount1 = amount1.add(dummy.getAmount1());
            if (dummy.getAmount2() != null) amount2 = amount2.add(dummy.getAmount2());
            add(dummy);
        }

        public Accumulator merge(Accumulator another) {
            newDummyList.addAll(another.newDummyList);
            return this;
        }

        public Accumulator mergeAndSum(Accumulator another) {
            amount1 = amount1.add(another.amount1);
            amount2 = amount2.add(another.amount2);
            return merge(another);
        }

        public DummyObject finish() {
            return someCondition ?
                new DummyObject(newDummyList, amount1, amount2) :
                new DummyObject(newDummyList);
        }
    }

    return Collector.of(
        Accumulator::new, 
        someCondition ? Accumulator::addAndSum : Accumulator::add,
        someCondition ? Accumulator::mergeAndSum : Accumulator::merge,
        Accumulator::finish);
}

Now we're ready to go:

dummy.stream().collect(toDummyObject(
    eachDummy -> mapper.map(eachDummy, NewDummy.class), 
    someCondition));
like image 69
fps Avatar answered Dec 25 '22 23:12

fps