如何检查 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.94]。 即使值 199.04 是 present.it 转到其他部分

两个值都是different.Net养老金is:199.04

当调用 ArrayList 的 contains 方法时,它会对所有包含的元素执行 equals() 调用。这里可能发生的是 class BigDecimal 的实例与浮点类型的值相比不正确。

考虑进入 contains() 并检查在与 BigDecimal 比较时对浮点值做了什么。 另一种可能的解决方案是自己将浮点值转换为 BigDecimal。

我相信,这与

有关

不是将 BigDecimal 存储到列表中,而是将 double 值四舍五入到 2 个位置,这将使您的处理更容易。下面给出了一个示例程序:

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

数据库中的所得税值与屏幕上的值匹配