【问题标题】:How to check if value is present in list in java如何检查值是否存在于java中的列表中
【发布时间】:2020-03-31 12:11:51
【问题描述】:

我想在 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 存在。它转到 else 部分

两者的值不同。净养老金是:199.04

【问题讨论】:

  • NetPension 是什么类型的?该类是否实现了equals和hashcode/comparable?您应该考虑遵循命名约定,这将使您的代码更易于阅读。
  • List&lt;BigDecimal&gt; 永远不会包含任何float。投射其中一个
  • 你将转换后的浮点值更改为 bigdecimal 并且它起作用了。谢谢你

标签: java list selenium contains


【解决方案1】:

我相信,和How to retrieve values from Result set and use it for calculations有关

不要将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

【讨论】:

    【解决方案2】:

    将 NetPension 转换为 BigDecimal:

        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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-11-04
        • 2018-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-22
        • 1970-01-01
        • 2019-11-02
        相关资源
        最近更新 更多