第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

使用 Java 8 流,如何查找特定時(shí)間段內(nèi)具有特定條件的項(xiàng)目

使用 Java 8 流,如何查找特定時(shí)間段內(nèi)具有特定條件的項(xiàng)目

郎朗坤 2022-08-17 10:49:00
我需要幫助來了解如何在java中找到一些目前正在sql中完成的東西。我需要使用流在特定持續(xù)時(shí)間內(nèi)的列表中查找特定數(shù)據(jù)場景:我有一個(gè)帶有字符串 CustId、即時(shí)時(shí)間戳、雙倍 betAmount 的 Bet 對象我需要找到所有超過每24小時(shí)100.00限制的客戶,在Java 8中該怎么做?該方法是public List<String> findLimits(List<Bet> bets){...}示例數(shù)據(jù) :note: to be parsed in List<Bet>A00001    2019-01-15T02:01:10   43.00A00001    2019-01-15T04:00:00   13.00A00001    2019-01-15T04:01:15   50.00B00034    2019-01-15T05:00:00   15.00A00001    2019-01-15T06:56:20   5.00B00034    2019-01-15T06:57:00   20.00C00004    2019-01-15T07:01:00   90.00C00004    2019-01-15T07:11:00   30.00B00034    2019-01-17T01:00:00   90.00預(yù)期結(jié)果:["A00001","C00004"] (List<String>)注意:該列表將包含所有帶有差異 cust id 和時(shí)間順序時(shí)間戳的投注滑動的24小時(shí)周期和組合客戶組合是我試圖解決的棘手問題。
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

您可以使用投注金額的總和來映射日期。然后過濾它們。

public List<String> findLimits(List<Bet> bets) {    return bets.stream()
            .collect(Collectors.toMap(
                    b -> b.getCustId() + LocalDate.ofInstant(b.getTimestamp(), ZoneOffset.UTC).toString(),
                    Bet::getAmount,
                    (o1, o2) -> o1 + o2))
            .entrySet().stream()
            .filter(e -> e.getValue() > 100.0)
            .map(e -> e.getKey().substring(0, e.getKey().length() - LocalDate.EPOCH.toString().length()))
            .collect(Collectors.toList());
}


查看完整回答
反對 回復(fù) 2022-08-17
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先,您可以按客戶ID對數(shù)據(jù)進(jìn)行分組,然后在24小時(shí)內(nèi)分析總和。正如你所提到的,記錄是按日期升序排序的,所以方法可以如下所示:findLimits


class Bet {

    String ID;

    LocalDateTime dateTime;

    BigDecimal value;

}


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

    BigDecimal sumLimit = new BigDecimal(100);

    Map<String, List<Bet>> map = new HashMap<String, List<Bet>>();

    List<String> result = new ArrayList<String>();

    for (Bet bet : bets) {

        if (map.get(bet.ID) == null)

            map.put(bet.ID, new ArrayList<Bet>());

        map.get(bet.ID).add(bet);

    }


    for (String ID : map.keySet()) {

        List<Bet> betListForCustomer = map.get(ID);

        boolean customerExceededLimit = false;

        for (int i = 0; i < betListForCustomer.size(); i++) {

            LocalDateTime endOfPeriod = betListForCustomer.get(i).dateTime.plusDays(1); //calculating end of 24h period current data

            BigDecimal sum = new BigDecimal(0);

            for (int j = i; j < betListForCustomer.size() //move start period to next dateTime

                    && endOfPeriod.isAfter(betListForCustomer.get(j).dateTime); j++) { //analyzing to the last date in 24h period or end data set

                sum = sum.add(betListForCustomer.get(j).value);

            }

            if (sum.compareTo(sumLimit) >= 0) { //sum >= 100

                customerExceededLimit = true;

                break; //there is no need to analyze this customer, limit exceeded

            }

        }

        if (customerExceededLimit) {

            result.add(ID);

        }

    }

    return result;

}


查看完整回答
反對 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 112 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號