使用 Java 8 流,如何查找特定时间段内具有特定条件的项目

我需要帮助来了解如何在java中找到一些目前正在sql中完成的东西。我需要使用流在特定持续时间内的列表中查找特定数据


场景:我有一个带有字符串 CustId、即时时间戳、双倍 betAmount 的 Bet 对象


我需要找到所有超过每24小时100.00限制的客户,在Java 8中该怎么做?


该方法是


public List<String> findLimits(List<Bet> bets){

...

}

示例数据 :


note: to be parsed in List<Bet>


A00001    2019-01-15T02:01:10   43.00

A00001    2019-01-15T04:00:00   13.00

A00001    2019-01-15T04:01:15   50.00

B00034    2019-01-15T05:00:00   15.00

A00001    2019-01-15T06:56:20   5.00

B00034    2019-01-15T06:57:00   20.00

C00004    2019-01-15T07:01:00   90.00

C00004    2019-01-15T07:11:00   30.00

B00034    2019-01-17T01:00:00   90.00

预期结果:


["A00001","C00004"] (List<String>)

注意:该列表将包含所有带有差异 cust id 和时间顺序时间戳的投注


滑动的24小时周期和组合客户组合是我试图解决的棘手问题。


郎朗坤
浏览 89回答 2
2回答

慕码人2483693

您可以使用投注金额的总和来映射日期。然后过滤它们。public&nbsp;List<String>&nbsp;findLimits(List<Bet>&nbsp;bets)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;bets.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toMap( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;->&nbsp;b.getCustId()&nbsp;+&nbsp;LocalDate.ofInstant(b.getTimestamp(),&nbsp;ZoneOffset.UTC).toString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bet::getAmount, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(o1,&nbsp;o2)&nbsp;->&nbsp;o1&nbsp;+&nbsp;o2)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.entrySet().stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(e&nbsp;->&nbsp;e.getValue()&nbsp;>&nbsp;100.0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(e&nbsp;->&nbsp;e.getKey().substring(0,&nbsp;e.getKey().length()&nbsp;-&nbsp;LocalDate.EPOCH.toString().length())) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList()); }

阿波罗的战车

首先,您可以按客户ID对数据进行分组,然后在24小时内分析总和。正如你所提到的,记录是按日期升序排序的,所以方法可以如下所示:findLimitsclass Bet {&nbsp; &nbsp; String ID;&nbsp; &nbsp; LocalDateTime dateTime;&nbsp; &nbsp; BigDecimal value;}public List<String> findLimits(List<Bet> bets) {&nbsp; &nbsp; BigDecimal sumLimit = new BigDecimal(100);&nbsp; &nbsp; Map<String, List<Bet>> map = new HashMap<String, List<Bet>>();&nbsp; &nbsp; List<String> result = new ArrayList<String>();&nbsp; &nbsp; for (Bet bet : bets) {&nbsp; &nbsp; &nbsp; &nbsp; if (map.get(bet.ID) == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(bet.ID, new ArrayList<Bet>());&nbsp; &nbsp; &nbsp; &nbsp; map.get(bet.ID).add(bet);&nbsp; &nbsp; }&nbsp; &nbsp; for (String ID : map.keySet()) {&nbsp; &nbsp; &nbsp; &nbsp; List<Bet> betListForCustomer = map.get(ID);&nbsp; &nbsp; &nbsp; &nbsp; boolean customerExceededLimit = false;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < betListForCustomer.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LocalDateTime endOfPeriod = betListForCustomer.get(i).dateTime.plusDays(1); //calculating end of 24h period current data&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BigDecimal sum = new BigDecimal(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = i; j < betListForCustomer.size() //move start period to next dateTime&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; && endOfPeriod.isAfter(betListForCustomer.get(j).dateTime); j++) { //analyzing to the last date in 24h period or end data set&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum = sum.add(betListForCustomer.get(j).value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sum.compareTo(sumLimit) >= 0) { //sum >= 100&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; customerExceededLimit = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; //there is no need to analyze this customer, limit exceeded&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (customerExceededLimit) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(ID);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return result;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java