Home > other >  Is there a way to modify below code using java8 map
Is there a way to modify below code using java8 map

Time:12-08

I am new to java 8, I want to modify my old version code using java 8 stream map but I am not able to modify below piece of code using java 8 map. Is it possible to modify the code using java 8.

private Item getItemManufacturerPriceCodes(Item item) {

    List<ItemPriceCode> itemPriceCodes = item.getItemPriceCodes();

    for(ItemPriceCode ipc : itemPriceCodes) {

        Optional<ManufacturerPriceCodes> mpc = manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED);
        
        if (mpc.isPresent())
            ipc.setManufacturerPriceCode(mpc.get().getName());
    }

    item.getItemPriceCodes()
            .removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));

    return item;
}

I tried lot of things but this function not able to modify. I declare the jpa query line above for each loop and map the list of PriceCodes but not able to get the exact result. which is produced by above function. How to modify above function using java 8 map stream , all the data comes from data base. How cam I modify this function.

CodePudding user response:

itemPriceCodes.stream()
.filter(create method which take the output of  manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED)  and return boolan and call it here as
ipc -> themethod(manufacturerPriceCodesRepository.findByManufacturerIDAndPriceCodeAndRecordDeleted(item.getManufacturerID(), ipc.getPriceCode(), NOT_DELETED)))
.forEach(ipc -> ipc.setManufacturerPriceCode(mpc.get().getName()));

the above for the loop part

here some resources https://dev.java/learn/the-stream-api/

i know this should be comment but this new account

hope that help and have a nice day :)

CodePudding user response:

I would rather stick with imperative logic like the one you've provided because the main purpose of for-loop in the original code is changing the state of your domain objects.

That means that you need either to perform side-effects via Stream.forEach(), which according to the Stream API documentation should be used with caution as a last resort (and since there's an alternative way, namely for-loop, it's not justifiable to use forEach() on a stream), or the second approach would be to materialize the Optional values returned by your Repository as a Collection (that would increase memory consumption, therefore again my advice is to stick with the original code).

Purely for educational purposes, here's an example of how the latter option can be implemented, using Collector toMap() to associate each ItemPriceCode with the corresponding Optional<ManufacturerPriceCodes>.

A combination of Iterable.forEach() and Optional.ifPresent() is used to modify the state of ItemPriceCode instances.

private Item getItemManufacturerPriceCodes(Item item) {
    
    Map<ItemPriceCode, Optional<ManufacturerPriceCodes>> mpcList =
        item.getItemPriceCodes().stream()
            .collect(Collectors.toMap(
                Function.identity(),                  // keyMapper
                ipc -> foo(ipc),                      // valueMapper
                (left, rihgt) -> left.or(() -> rihgt) // mergeFunction - both parameters are of type Optional<ManufacturerPriceCodes>, left.or(() -> rihgt) replaces `left` with `right` if the left is empty
            ));
    
    mpcList.forEach((ipc, optionalMpc) ->
        optionalMpc.ifPresent(mpc -> ipc.setManufacturerPriceCode(mpc.getName()))
    );
    
    item.getItemPriceCodes()
        .removeIf(ipc -> DELETED.equals(ipc.getRecordDeleted()));
    
    return item;
}

Note:

  • lengthy method call on the Repository, reflected in the code above as foo(ipc).

  • if all ItemPriceCode instances are unique, the third argument of the Collector toMap(), merge Function, is not required and can be removed.

  • Related