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

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

Java 8中通過多個(gè)匹配值獲取列表的對(duì)象

Java 8中通過多個(gè)匹配值獲取列表的對(duì)象

米琪卡哇伊 2022-05-21 13:52:19
我正在嘗試按最高值匹配進(jìn)行規(guī)則過濾。我有一個(gè)包含多個(gè)產(chǎn)品的銷售,我必須對(duì)每個(gè)產(chǎn)品應(yīng)用不同的規(guī)則。獲得此結(jié)果的最佳方法是什么?List<Rule> rules = listOfRules();String system = "MySystem1";Map<Product, Rule> mapOfProductRule = new HashMap<Product, Rule>();sale.getProducts().forEach(product -> {    int points = 0;    Rule matchedRule = null;    for (Rule rule : rules) {        if (system == rule.getSystem()) {            int countMatchs = 0;            if (sale.getValue1() == rule.getValue1()) countMatchs++;            if (sale.getValue2() == rule.getValue2()) countMatchs++;            if (product.getPvalue1() == rule.getPvalue1()) countMatchs++;            if (product.getPvalue2() == rule.getPvalue2()) countMatchs++;            if (countMatchs!= 0 && points < countMatchs)            {                points = countMatchs;                matchedRule = rule;            }        }    }    mapOfProductRule.put(product, matchedRule);});return mapOfProductRule;
查看完整描述

1 回答

?
德瑪西亞99

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

首先,我會(huì)將所有 4 if.. 移動(dòng)到Rule類中的一個(gè)方法中


public int getScore(Sale sale, Product product) {

    int count = 0;

    if (this.getValue1() == sale.getValue1()) count++;

    //...

    return count;

}

然后我會(huì)rules用


Rule bestRule = rules.stream().max(Comparator.comparingInt(r -> r.getScore(sale, product)));



查看完整回答
反對(duì) 回復(fù) 2022-05-21
  • 1 回答
  • 0 關(guān)注
  • 222 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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