Problem with grouping with multiple columns? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Problem with grouping with multiple columns? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have this method:

public List<IncomeChannelCategoryMap> allIncomeChannels(final List<String> list) {
    final CriteriaQuery<IncomeChannelCategoryMap> criteriaQuery = builder.createQuery(IncomeChannelCategoryMap.class);
    final Root<IncomeChannelMapEntity> root = criteriaQuery.from(IncomeChannelMapEntity.class);

    final List<Selection<?>> selections = new ArrayList<>();
    selections.add(root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code));
    selections.add(root.get(IncomeChannelMapEntity_.logicalUnitCode));
    selections.add(root.get(IncomeChannelMapEntity_.logicalUnitIdent));
    selections.add(root.get(IncomeChannelMapEntity_.keyword));
    criteriaQuery.multiselect(selections);
    Predicate codePredicate = root.get(IncomeChannelMapEntity_.incomeChannel).get(IncomeChannelEntity_.code).in(list);
    criteriaQuery.where(codePredicate);
    return entityManager.createQuery(criteriaQuery).getResultList();

}

And this:

   @Override
    public List<IncomeChannelCategoryMap> allIncomeChannels(final EntityRequest<IncomeChannel> request) throws ApiException {
        List<String> lists = request.getEntity().getIncomeChannels();
        List<IncomeChannelCategoryMap> channels = incomeChannelMapDAO.allIncomeChannels(lists);
        return new ArrayList<>(channels.stream().collect(Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode,
                Function.identity(), (final IncomeChannelCategoryMap i1, final IncomeChannelCategoryMap i2) -> {
                    i1.setLogicalUnitIdent(i1.getLogicalUnitIdent() + "," + i2.getLogicalUnitIdent());
                    return i1;
                })).values());

    }

I am able to achieve this:

  {
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "7,8"
  }

from

[
{
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "7"
  },
  {
    "incomeChannelCode": "DIRECT_SALES",
    "logicalUnitCode": "R_CATEGORY",
    "logicalUnitIdent": "8"
  }
]

And everything is great but have one problem:

For example DIRECT_SALES can have another logicalUnitCode so right now im getting only one, and i want to achive for logicalUnitCode like i did for logicalUnitIdent.

Any suggestion? So what I want to achieve is this:

      {
        "incomeChannelCode": "DIRECT_SALES",
        "logicalUnitCode": "R_CATEGORY","R_TYPE",
        "logicalUnitIdent": "7,8"
      }

Answer

Here is your updated code:

@Override
public List<IncomeChannelCategoryMap> allIncomeChannels(final EntityRequest<IncomeChannel> request) throws ApiException {
    List<String> lists = request.getEntity().getIncomeChannels();
    List<IncomeChannelCategoryMap> channels = incomeChannelMapDAO.allIncomeChannels(lists);
    return new ArrayList<>(channels.stream().collect(Collectors.toMap(IncomeChannelCategoryMap::getIncomeChannelCode,
            Function.identity(), (i1, i2) -> {
                i1.setLogicalUnitIdent(i1.getLogicalUnitIdent() + ", " + i2.getLogicalUnitIdent());
                if (!i1.getLogicalUnitCode().contains(i2.getLogicalUnitCode())) {
                    i1.setLogicalUnitCode(i1.getLogicalUnitCode() + ", " + i2.getLogicalUnitCode());
                }
                return i1;
            })).values());
}

Just like logicalUnitIdent now logicalUnitCode will also be grouped. Here I’m assuming that you don’t want duplicates here. By duplicates I mean if logicalUnitCode is "R_CATEGORY" for both the results then you want it once as output. And if one is "R_CATEGORY" and the other one is "R_TYPE" then you want them to be grouped as "R_CATEGORY, R_TYPE" as output. If my assumption is correct then this is your required answer.

We are here to answer your question about Problem with grouping with multiple columns? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji