0

Problem Statement : Let's say I've list of PriceRow(productCode, key, sector) objects

        List<PriceRow> priceRowList = new ArrayList<>();
        priceRowList.add(new PriceRow("10kgbag","", "SECTOR"));
        priceRowList.add(new PriceRow("10kgbag","12345", ""));
        priceRowList.add(new PriceRow("10kgbag","", ""));
        priceRowList.add(new PriceRow("20kgbag","", "SECTOR"));
        priceRowList.add(new PriceRow("20kgbag","12345", ""));
        priceRowList.add(new PriceRow("20kgbag","", ""));
        priceRowList.add(new PriceRow("30kgbag","", "SECTOR"));
        priceRowList.add(new PriceRow("30kgbag","", ""));
        priceRowList.add(new PriceRow("40kgbag","", ""));
        priceRowList.add(new PriceRow("50kgbag","", ""));

Now, I need to group it by productCode, and then sort it on the basis of first key then sector, if both rows are not available then take the row with (key = blank) and (sector=blank) and now take the first row out of the sorted list to create a Map<String, PriceRow)

Hence the final assertions should look like

assertEquals("12345",map.get("10kgbag").getFlightKey());
assertEquals("12345",map.get("20kgbag").getFlightKey());
assertEquals("SECTOR",map.get("30kgbag").getSector());  
assertEquals("",map.get("40kgbag").getFlightKey());     
assertEquals("",map.get("50kgbag").getFlightKey());

The solution I came up with is

import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Example {
    public Map<String,PriceRow> evaluate(List<PriceRow> priceRowList) {
        Map<String,PriceRow>  map =  priceRowList.stream()
                .collect(Collectors.groupingBy(priceRow -> priceRow.getProductCode(),
                        Collectors.collectingAndThen(Collectors.toList(), value -> getMostEligibleValue(value))));
        return map;
    }


    private PriceRow getMostEligibleValue(List<PriceRow> priceRowList){
        for(PriceRow priceRowWithKey : priceRowList)
            if(StringUtils.isNotBlank(priceRowWithKey.getKey()))
                return priceRowWithKey;

        for(PriceRow priceRowWithSector : priceRowList)
            if(StringUtils.isNotBlank(priceRowWithSector.getSector()))
                return priceRowWithSector;

        return priceRowList.stream().findFirst().get();
    }
}

Hope I'm able to explain the problem statement. If there are any better solutions for this problem, pls let me know. Thanks in advance for your help.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
rowen
  • 37
  • 5
  • What's the issue you are facing now? The problem statement is also not clear? – Ravindra Ranwala May 20 '18 at 11:43
  • 1
    So your code is working? If you want commentary and criticism on working code, you should use codereview.stackexchange.com, not StackOverflow. – Robin Green May 20 '18 at 11:51
  • As you can see, I'm not sorting the list in my solution, I'm rather going through each value and then picking first the key then sector n then default one. Just wanted to check if this could be done with custom sorting after grouping by and then by picking first value out of the sorted list? – rowen May 20 '18 at 11:54
  • 1
    @RobinGreen The code is working, but I just want to check if there are any better solution for this problem. – rowen May 20 '18 at 15:00
  • @RobinGreen Feel free to recommend the OP post on CR but in the future, please don't use Code Review as a reason to close a question. Evaluate the request and use a reason like *too broad*, *primarily opinion-based*, etc. Then you can mention to the OP that it can be posted on Code Review if it is [on-topic](https://codereview.stackexchange.com/help/on-topic). Please see the section **What you should not do** in [this answer to _A guide to Code Review for Stack Overflow users_](https://codereview.meta.stackexchange.com/a/5778/120114) – Sᴀᴍ Onᴇᴌᴀ May 21 '18 at 22:38
  • My hunch is you should sort first, then do a `toMap` with a clever merge function. https://stackoverflow.com/questions/32312876/ignore-duplicates-when-producing-map-using-streams. Actually sorting might not be necessary but you'll need a comparison (or Comparator) in there somewhere. GL! – rogerdpack May 07 '21 at 21:30

0 Answers0