Q:

Add ArrayList items to a new ArrayList comparing a value

I have a ArrayList (invProperty) which has two fields invoice_no and invoice_qty. I want to add items to a new ArrayList (newProperty) where invoice_no equals to a value (invNumber). I am checking with the first ArrayList. I have come up with two approaches and I want to know which one is better, or is there any other solutions?
invProperty is the first ArrayList and newProperty is the new ArrayList. invNumber is the value I am checking,
for (InvoiceSubData s : invProperty) {
    if (invNumber.equals(s.getInvoiceNo())) {
        newProperty.add(s);
    }
}

for (InvoiceSubData s : invProperty) {
    if (s.getInvoiceNo().contains(invNumber)) {
        newProperty.add(s);
    }
}

A:

According to your criteria you should use the first one. Second one could give you unwanted result.
For example: if your invoice number is "41235" and there is a invoice number in the list have "123" then it will generate true ("41235".contains("123")) and added to your newly created list.
If you could use Java 8 in your project you can also do this
newProperty = invProperty.stream()
                       .filter(item -> invNumber.equals(item.getInvoiceNo()))
                      .collect(Collectors.toList());

