Prince Kumar
Prince Kumar

Reputation: 39

String matching within nested for loop

Lets say i have two different Strings of given pojo type.

 List 1 = "one", "two", "three", "four", "five".
 List 2 = "one", "two", "four", "five".

I wants to retrieve a string that doesn't found in another list and add it into another list(say List 3). how can i do this?

i'm trying this:-

 for ( SettlementReportNB showSellementReport : settlementReportList )
        {
            String merchantreferencenumber = showSellementReport.getMerchantreferencenumber();
            for ( AllTransactions showAllTransaction : allTransactionsList )
            {
                String merchantTxnId = showAllTransaction.getMerchantTxnId();
                if ( !merchantreferencenumber.equals( merchantTxnId ) )
                {
                    idNotFound.add( merchantTxnId );
                }
            }
        }

but it is not giving me an expected answer.

Upvotes: 1

Views: 303

Answers (7)

poyger
poyger

Reputation: 666

List a = new ArrayList(asList("one", "two", "three", "four", "five"));
List b = new ArrayList(asList("one", "two", "four", "five"));
List c = new ArrayList();
c.addAll(a);
c.addAll(b);
c.removeIf(o -> a.contains(o) && b.contains(o));

Upvotes: 0

Guilherme Bernert
Guilherme Bernert

Reputation: 11

If you need to verify all the elements that are not present in the second list only, just create a Set and use the !contains from the first list, if you need to verify both lists then add the second for from the example below:

Set<String> list3 = new HashSet<String>();
for (String text : list1) {
  if (!list2.contains(text)) list3.add(text);
}
for (String text : list2) {
  if (!list1.contains(text)) list3.add(text);
}

Upvotes: 1

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

Or you could something like this:

List list1 = Arrays.asList("one", "two", "three", "four", "five");
List list2 = Arrays.asList("one", "two", "four", "five");

//create a clone of list1
List list3 = new ArrayList<>(list1);

//remove all elements of list2 from list1
list3.removeAll(list2);

//output : [three]
System.out.print(list3);

You will end up wit list3 that has only [three]

Or try something like this to keep your existing code :

for ( SettlementReportNB showSellementReport : settlementReportList ) {

    String allTransactionsList = showSellementReport.getMerchantreferencenumber();

    if ( isNotContain(allTransactionsList, allTransactionsList))
    {
        idNotFound.add( merchantTxnId );
    }

}

private boolean isNotContain(List allTransactionsList, String merchantreferencenumber) {

    for ( AllTransactions showAllTransaction : allTransactionsList )
    {
        String merchantTxnId = showAllTransaction.getMerchantTxnId();
        if ( merchantreferencenumber.equals( merchantTxnId ) )
        {
            return false;
        }
    }

    return true;
}

Upvotes: 1

Binyamin Regev
Binyamin Regev

Reputation: 1030

If you are using Java 8 then use Lambdas - it will be much faster and will save you a loop.

Upvotes: 0

Boris Zhguchev
Boris Zhguchev

Reputation: 314

May be this case

        List<String> a1 = new ArrayList<>(Arrays.asList( "one", "two", "three", "four", "five"));
        List<String> a2 = new ArrayList<>(Arrays.asList( "one", "two", "four", "five"));

        Set<String> s1 = new HashSet<>(a1);
        Set<String> s2 = new HashSet<>(a2);

        s2.removeAll(s1);

        List<String> a3 = new ArrayList<>(s2);

        List<String> result =  new ArrayList<>(Arrays.asList( "six"));
        result.addAll(a3);

Upvotes: 0

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37594

Your second for loop does not check every element. It checks only if one element does not match the compared String. You have to iterate over the whole list before you are sure it's not already present e.g.

for (SettlementReportNB showSellementReport: settlementReportList) {

    boolean notPresent = false;
    String merchantTxnId = null;
    String merchantreferencenumber = showSellementReport.getMerchantreferencenumber();
    for (AllTransactions showAllTransaction: allTransactionsList) {
        merchantTxnId = showAllTransaction.getMerchantTxnId();
        if (merchantreferencenumber.equals(merchantTxnId)) {
            notPresent = false;
            break; // get out of loop because it already exists
        } else {
            notPresent = true;
        }
    }
    if (notPresent) idNotFound.add(merchantTxnId);
}

Or you could use any of the other answers with List.util functions

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

You are adding the string to result too soon. Your code has to wait for the loop to finish before calling idNotFound.add();

Define a boolean variable notFound, and set it to true before the loop. If you find a match in the loop, set the variable to false, and break out of the loop.

If the variable remains true after the loop is over, call idNotFound.add();

Upvotes: 2

Related Questions