簡體   English   中英

如何檢查值是否存在於java中的列表中

[英]How to check if value is present in list in java

我想在 TaxList 中匹配這個值 199.04。 下面是我的代碼

List<BigDecimal> TaxList = new ArrayList<BigDecimal>();   

String NetP=driver.findElement(getNetPension).getAttribute("value");
float NetPension = new Float(NetP);
log.info("Value of Net Pension on screen : "+ NetPension);

if(TaxList.contains(NetPension)) 
{   
     log.info("Income tax value from the database is matching with the value on the screen");
}
else 
{
     log.warn("Both the values are different.Net pension is:" +NetPension );
}

我正在控制台中打印 TaxList

[199.04, 610.72, 122.12, 866.52, 94.56, 143.48, 78.28, 132.6, 12.9, 32.03, 1797.38, 128.14, 724.9] 即使值 199.04 存在。它轉到其他部分

兩者值不同。凈養老金為:199.04

在 ArrayList 上調用 contains 方法時,它對所有包含的元素執行equals()調用。 這里可能發生的情況是 BigDecimal 類的實例沒有正確地與浮點類型的值進行比較。

考慮進入contains()並檢查在與 BigDecimal 進行比較時對浮點值做了什么。 另一種可能的解決方案是自己將浮點值轉換為 BigDecimal。

我相信,它與如何從結果集中檢索值並將其用於計算有關

不是將BigDecimal存儲到列表中,而是存儲四舍五入到 2 位的double值,這將使您的處理更容易。 下面給出了一個示例程序:

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

class Main {
    public static void main(String[] args) {
        List<Double> taxList = new ArrayList<Double>();
        taxList.add(new BigDecimal(String.valueOf(610.72123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(122.0072123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(199.04123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(78.28123)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        taxList.add(new BigDecimal(String.valueOf(724.94507)).setScale(2,RoundingMode.HALF_UP).doubleValue());
        System.out.println(taxList);

        float f = 199.04f;
        double netPension = BigDecimal.valueOf(f).setScale(2,RoundingMode.HALF_UP).doubleValue();
        System.out.println(netPension);

        if (taxList.contains(netPension)) {
            System.out.println("Income tax value from the database is matching with the value on the screen");
        } else {
            System.out.println("Both the values are different. Net pension is:" + netPension);
        }
    }
}

輸出:

[610.72, 122.01, 199.04, 78.28, 724.95]
199.04
Income tax value from the database is matching with the value on the screen

在 BigDecimal 中轉換 NetPension :

    List<BigDecimal> TaxList = new ArrayList<BigDecimal>();   

    TaxList.add(new BigDecimal(584.32));
    TaxList.add(new BigDecimal(199.04));
    TaxList.add(new BigDecimal(201.66));

    double NetPension = 199.04;

    System.out.println("Value of Net Pension on screen : "+ NetPension);

    if(TaxList.contains(new BigDecimal(NetPension))) 
    {   
        System.out.println("Income tax value from the database is matching with the value on the screen");
    }
    else 
    {
        System.out.println("Both the values are different.Net pension is:" +NetPension );
    }

輸出 :

屏幕上的凈養老金值:199.04

數據庫中的所得稅值與屏幕上的值匹配

暫無
暫無

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

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