如何检查ArrayList中是否存在值?

时间:2018-03-14 10:08:47

标签: java

using (SqlConnection connection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=Northwind"))  
{  
    connection.Open();        
// Pool A is created.  
}  

using (SqlConnection connection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=pubs"))  
{  
    connection.Open();        
// Pool B is created because the connection strings differ.  
}  

using (SqlConnection connection = new SqlConnection("Integrated Security=SSPI;Initial Catalog=Northwind"))  
{  
    connection.Open();        
// The connection string matches pool A.  
}  

输出:

try {                   
    List<UpdateStockModel> 
    stockistIds=updateStockMapper.getStockInvoiceId(stockmodel);
    String myList = new String();
    for (UpdateStockModel x : stockistIds) {
        //System.out.println("stockist list id.." + x.getStockInvoiceIds());    

        myList = myList.concat(x.getStockInvoiceIds());
        myList = myList.concat(",");

    }   

    List<String> list = new ArrayList<String>();
    list.add(myList);   
    System.out.println("list.." + list);    
    System.out.println("stockInvoiceId is.." + 
    stockmodel.getStockInvoiceIds());
    System.out.println("list status.." +list.contains(stockmodel.getStockInvoiceIds()));
    if (list.contains(stockmodel.getStockInvoiceIds()) ==true){         
        return true;    
    } else {
        return true;           
    }
}

这里66存在于列表中但返回false。我需要得到真实

6 个答案:

答案 0 :(得分:3)

好的,让我们直截了当。 你有一个列表,其中包含一个包含ID的对象。 您从对象获取ID并将它们连接到一个大的String。 稍后,将此单个String添加到ArrayList,并期望List.contains()方法为您找到适当的匹配项。这它是如何工作的。

你可以通过调用list.get(0).contains(...)来解决这个问题,因为你将从列表中检索你的字符串并检查它的ID,甚至更好,你可以将它们自己添加到ArrayList中。

这样做最终会与此类似:

 List<UpdateStockModel> 
    stockistIds=updateStockMapper.getStockInvoiceId(stockmodel);
    List<String> myList = new ArrayList<>();
    for (UpdateStockModel x : stockistIds) { 
        myList.add(x.getStockInvoiceIds()); 
    }   

这样做将取代以下部分:

//This all becomes useless since you will already have a list with proper objects. 
List<String> list = new ArrayList<String>();
    list.add(myList);   
    System.out.println("list.." + list);    
    System.out.println("stockInvoiceId is.." + 
    stockmodel.getStockInvoiceIds());
    System.out.println("list status.." +list.contains(stockmodel.getStockInvoiceIds()));

这不是火箭科学。

想想列表,因为它们只是更具动态性和灵活性的阵列。

答案 1 :(得分:2)

我认为这就是你所追求的:

Set<String> ids = updateStockMapper.getStockInvoiceId(stockmodel)
        .stream()
        .map(usm -> usm.getStockInvoiceIds())
        .collect(Collectors.toSet());
String id = stockmodel.getStockInvoiceIds();

return ids.contains(id);

答案 2 :(得分:0)

尝试将列表初始化如下:

List<String> list = new ArrayList<String>();
for (UpdateStockModel x : stockistIds) {
    //System.out.println("stockist list id.." + x.getStockInvoiceIds());    
    list.add(x.getStockInvoiceIds());
}   

然后你可以再次比较列表而不是字符串。

答案 3 :(得分:0)

// if the element that you want to check is of string type

    String value= "66";// you use element instead of 66
Boolean flag=false;
    if (list.contains(value)){         
        flag=true;    
    } else {
        flag=false;           
    }

//you can use flag where you want

答案 4 :(得分:-1)

看起来您将列表变量设置为具有一个条目的Arry。

替换声明

list.contains(..)

stockistIds.contains(..)

应该这样做。

答案 5 :(得分:-4)

问题出在if声明中 变化:

if (list.contains(stockmodel.getStockInvoiceIds()) ==true){         
        return true;    
    } else {
        return true;           
    }

为:

if (list.contains(stockmodel.getStockInvoiceIds()) ==true){         
        return true;    
    } else {
        return false;           
    }