1

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.

  • Answer should be "three" because it is not present in another list.
3
  • 1
    what is your expected result and what are you getting right now Commented Feb 16, 2017 at 12:37
  • Answer should be "three" Commented Feb 16, 2017 at 12:40
  • The answer here exposes a simple way of doing set operations on lists stackoverflow.com/questions/3590677/… Commented Feb 16, 2017 at 12:41

7 Answers 7

2

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();

Sign up to request clarification or add additional context in comments.

Comments

1

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;
}

2 Comments

what if user wants the original list1 contents?
easy check the post again (create a new copy of list1)
1

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);
}

Comments

0

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

Comments

0

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);

Comments

0

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

Comments

0
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.