簡體   English   中英

Java比較多個值並找到最佳匹配

[英]java compare multiple values and find best match

我需要在數據庫記錄中找到最匹配的員工工資:

Name:   City:     State:

A       (null)    (null)

A       (null)    DEL

(null)  (null)    (null)

A        SAKET    DEL

比賽順序應為:

1. NAME =名稱,STATE =州,CITY =城市

2. NAME =名稱,STATE =狀態,CITY = NULL

3.名稱=名稱,狀態=空,城市=空

4.名稱= NULL,狀態= NULL,城市= NULL

表示如果所有屬性都匹配的行–應該選擇它,如果我們沒有這種數據,我們應該轉到下一個最佳選項,例如將state和city選擇為NULL等。

我的代碼如下,可以給我正確的結果,但是我需要一種更有效的方法。

    private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
    BigDecimal salary = null;
    BigDecimal salaryWithState = null;
    BigDecimal salaryWithName = null;
    BigDecimal salaryWithNoMatch = null;
     while (results.next()) {

        String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
        String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
        String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;

        BigDecimal salaryRslt = null;

        if(results.getString("SALARY") != null){
            salaryRslt = BigDecimal.valueOf(results.getDouble("SALARY"));               
        }  
        if(billerName != null && !billerName.equals("") && billerName.equals(request.getBillPaymentsalaryCalculateInfo().getBillerName())){
            if(city != null && !city.equals("") && city.equals(request.getMsgRqHdr().getCity()) &&
                    state != null && !state.equals("") && state.equalsIgnoreCase(request.getMsgRqHdr().getstate())){
                salary = salaryRslt;
                break;
            } else if((city == null || city.equals("")) && state != null && !state.equals("") &&
                    state.equalsIgnoreCase(request.getMsgRqHdr().getState())){
                salaryWithState = salaryRslt;                   
            } else if((city == null || city.equals("")) && (state == null || state.equals(""))){
                salaryWithName = salaryRslt;                    
            }
        } else if((billerName == null || billerName.equals("")) && (city == null || city.equals("")) && 
                (state == null || state.equals(""))){
            salaryWithNoMatch = salaryRslt;             
        }
     }

     if(salary != null){
         return salary;
     } else if(salaryWithState != null){
         salary = salaryWithState;
     } else if(salaryWithName != null){
         salary = salaryWithName;
     } else if(salaryWithNoMatch != null){
         salary = salaryWithNoMatch;
     } 

     return salary;

}

編輯 :我不想使用3個額外的變量:SalaryWithState,SalaryWithName,SalaryWithNoMatch。

我只想給出一般的想法,如何實現,所以我還沒有實際測試過並檢查它是否能給您合適的薪水。

public BigDecimal getSalaryForBestMatch(ResultSet resultSet, PaymentSalaryInfo paymentSalaryInfo) {
    Map<String, Supplier<String>> m1 = new HashMap<>();
    m1.put("EMP_NAME", paymentSalaryInfo::getBillerName);
    m1.put("STATE", paymentSalaryInfo::getState);
    m1.put("CITY", paymentSalaryInfo::getCity);

    Map<String, Supplier<String>> m2 = new HashMap<>();
    m2.put("STATE", paymentSalaryInfo::getState);
    m2.put("CITY", paymentSalaryInfo::getCity);

    Map<String, Supplier<String>> m3 = new HashMap<>();
    m3.put("CITY", paymentSalaryInfo::getCity);


    Optional<String> salary = Optional.empty();

    while(resultSet.next() && !salary.isPresent()) {
        salary = apply(m1, resultSet);
        //check salary and then apply(m2, resultSet) ....
    }

    return salary.isPresent() ? new BigDecimal(salary.get()) : null;
}


public Optional<String> apply(Map<String, Supplier<String>> filter, ResultSet resultSet) {
    boolean allMatch = filter.entrySet().stream().allMatch(entry -> {
        String value = resultSet.getString(entry.getKey());

        return value != null && value.equals(entry.getValue().get());
    });

    return allMatch ? Optional.of(resultSet.getString("salary")) : Optional.empty();
}

我使用數組以不同的方式編寫了相同的邏輯。 如果您的環境可以使用數組,則可以使用此代碼。 但是我還沒有測試代碼。

 private static BigDecimal getsalaryForBestMatch(ResultSet results, EmployeeRq request) throws Exception{
    BigDecimal salary = null;
    int matchCount = 0;
    String rBillerName = request.getBillPaymentsalaryCalculateInfo().getBillerName();
    String rCity = request.getMsgRqHdr().getCity();
    String rState = request.getMsgRqHdr().getstate();

    String [] truthArray = new String[] {rBillerName, rCity, rState};

    while (results.next()) {
        String billerName = results.getString("EMP_NAME") != null ? results.getString("EMP_NAME").trim() : null;
        String city = results.getString("CITY") != null ? results.getString("CITY").trim() : null;
        String state = results.getString("STATE") != null ? results.getString("STATE").trim() : null;
        BigDecimal salaryRslt = results.getString("SALARY") != null ? BigDecimal.valueOf(results.getDouble("SALARY")): null;

        String [] testArray = new String[] {billerName, city, state};

        int localMatchCount = 0;
        for(int i = 0; i < testArray.length; i++) {
            if(testArray[i] != null && testArray[i].equals(truthArray[i]))
                localMatchCount++;
            else {
                break;
            }
        }

        if(localMatchCount >= matchCount){
            matchCount = localMatchCount;
            salary = salaryRslt;
        }
    }
    return salary;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM