Java 8 Lambda分组同时使用X和Y.

编程入门 行业动态 更新时间:2024-10-12 18:19:25
本文介绍了Java 8 Lambda分组同时使用X和Y.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在寻找一个lambda来优化已检索的数据。我有一个原始的结果集,如果用户没有更改我希望使用java的lambda按结果分组的日期。而且我是初次使用java的lambdas。

I'm looking for a lambda to refine the data already retrieved. I have a raw resultset, if the user do not change the date I want use java's lambda to group by the results for then. And I'm new to lambdas with java.

我正在寻找的lambda与此查询相似。

The lambda I'm looking for works simliar to this query.

select z, w, min(x), max(x), avg(x), min(y), max(y), avg(y) from table group by x, w;

推荐答案

所以我假设你有一个对象列表并且您想要创建具有给定分组的地图。我对你的x,y,w,z有点困惑,所以我会用自己的字段。但是我会这样做:

So I'm assuming you have a List of objects and you want to create a map with the given groupings. I am a bit confused by your x, y, w, z so I'll use my own fields. But Here's how I would do it:

interface Entry { String getGroup1(); String getGroup2(); int getIntData(); double getDoubleData(); } List<Entry> dataList; Map<String, Map<String, IntSummaryStatistics>> groupedStats = dataList.stream() .collect(Collectors.groupingBy(Entry::getGroup1, Collectors.groupingBy(Entry::getGroup2, Collectors.summarizingInt(Entry::getIntData))));

然后,如果你想获得A组,B组项目的平均数据,那么你使用:

Then if you want to get, say, the average of data for items with groups A, B then you use:

groupedStats.get("A").get("B").getAverage();

如果要同时汇总多组数据,那么它会变得有点复杂。您需要编写自己的包装类,可以累积多个统计信息。这是一个示例,其中包含Entry中的两个数据项(我将它们设为int和double使其更有趣)。

If you want to summarise more than one set of data simultaneously then it gets a bit more complicated. You need to write your own wrapper class that can accumulate multiple statistics. Here's an example with both data items in Entry (I made them an int and a double to make it a bit more interesting).

class CompoundStats { private final IntSummaryStatistics intDataStats = new IntSummaryStatistics(); private final DoubleSummaryStatistics doubleDataStats = new DoubleSummaryStatistics(); public void add(Entry entry) { intDataStats.accept(entry.getIntData()); doubleDataStats.accept(entry.getDoubleData()); } public CompoundStats combine(CompoundStats other) { intDataStatsbine(other.intDataStats); doubleDataStatsbine(other.doubleDataStats); return this; } }

然后可以使用此类创建自己的收藏家:

This class can then be used to create your own collector:

Map<String, Map<String, CompoundStats>> groupedStats = dataList.stream() .collect(Collectors.groupingBy(Entry::getGroup1, Collectors.groupingBy(Entry::getGroup2, Collector.of(CompoundStats::new, CompoundStats::add, CompoundStats::combine))));

现在你的地图返回一个CompoundStats而不是一个IntSummaryStatistics:

Now your maps return a CompoundStats instead of an IntSummaryStatistics:

groupedStats.get("A").get("B").getDoubleStats().getAverage();

另请注意,如果您创建了一个单独的类来保存您的分组而不是使用我上面提到的两步图。如果需要,再次不是一个困难的修改。

Also note that this would be neater if you created a separate class to hold your groupings rather than using the two step map I've proposed above. Again not a difficult modification if required.

希望这在你自己的情况下很有用。

Hopefully this is useful in your own case.

更多推荐

Java 8 Lambda分组同时使用X和Y.

本文发布于:2023-11-22 01:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615496.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!