【问题标题】:How to do grouping in java [closed]如何在java中进行分组[关闭]
【发布时间】:2018-02-07 11:02:14
【问题描述】:

我想基于 customerProduct 使用 java

做下面的数据 groupBy
[{customerID=123,customerName=John,customerProduct=mobile},
{customerID=456,customerName=roach,customerProduct=laptop},
{customerID=678,customerName=david,customerProduct=mobile},
{customerID=678,customerName=abd,customerProduct=charger}]

我们如何在 java 8 中做到这一点

期待如下

{
mobile= [
{customerID=123,customerName=John,customerProduct=mobile},
{customerID=678,customerName=david,customerProduct=mobile},
],
laptop= [{customerID=456,customerName=roach,customerProduct=laptop}],
charger= [{customerID=678,customerName=abd,customerProduct=charger}]
}

【问题讨论】:

  • 您对这个样本数据的预期结果是什么?
  • 我们没有您的信息。从我们的角度来看,我们不知道你想做什么。请添加一些上下文、一些示例等...此外,您应该将您的问题改写为“在 Java 中对数组进行排序”,因为对列表进行排序更容易。
  • 输出应该是这样的 { mobile= [ {customerID=123,customerName=John,customerProduct=mobile}, {customerID=678,customerName=david,customerProduct=mobile}, ], laptop= [ {customerID=456,customerName=roach,customerProduct=laptop}], 充电器= [{customerID=678,customerName=abd,customerProduct=charger}] }

标签: java arrays spring web-services collections


【解决方案1】:

应该这样做。

 stream().collect(Collectors.groupingBy(Customer::getCustomerProduct))

【讨论】:

  • 不使用pojo我们如何进行分组。请帮忙
  • 不使用 pojo 是什么意思。你必须给一个 pojo 的字段来分组。不然怎么分组。您将根据哪些价值观进行分组?
猜你喜欢
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 2014-03-10
  • 2022-08-18
  • 2021-08-15
  • 2012-05-19
相关资源
最近更新 更多