0

completely new to Java, I am trying to find the matched element from one array into another, cannot seem to understand how to do it. Here is a sample of how the data is and how far I've gotten:

In this code and after printing this line, this is how the data is:

ArrayList<String> all_accounts = new ArrayList<String>();
all_accounts.add(acc);
System.out.println("\nArray 1:" + all_accounts);

Result Array 1:

Array 1:[77737320]
Array 1:[88405378]
Array 1:[00056893]
Array 1:[10709816]

ArrayList<String> cancel_accounts = new ArrayList<String>();
cancel_accounts.add(cancel_acc);
System.out.println("\nArray 2:" + cancel_accounts);

Results from Array 2:

Array 2:[77737320]
Array 2:[]
Array 2:[]
Array 2:[]

Stack here, I still cant understand why it doesn't match:

     String found = null;
     for (String account: all_accounts) {

       for (String canceled: cancel_accounts) {
          System.out.println(canceled);
          found = canceled;
        }
     System.out.println(found);
     if(account.equals(found) ) {
              System.out.println(account);
         }

    }

I need to find the matched element, 77737320 in this case. Thanks for looking!

Andre
  • 819
  • 3
  • 15
  • 30
  • Possible duplicate of [How can I test if an array contains a certain value?](http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value) – Peter Perháč Nov 21 '16 at 19:20
  • @PeterPerháč The question is quite different, here the OP asks for array that contains more then one element not only one. – user6904265 Nov 21 '16 at 20:35
  • @user6904265 i insist that this question has definitely been answered here and OP can figure out how to do it looking at the other question i linked. this is "do my homework for me" kind of question and shouldn't be here – Peter Perháč Nov 21 '16 at 21:59

4 Answers4

3

+1 for answer from user6904265

However, You need not create a new HashSet. You can use ArrayList.retainAll(). If you want to maintain the all_accounts list, create a new clone and use that instead.

kartiks
  • 66
  • 4
  • I updated my answer with your hint. But if you don't want duplicate values (if input array contains any) in the final array you should use `Set`. ;) – user6904265 Nov 21 '16 at 20:06
1

You could implement this as intersection between sets:

Set<String> set_all_account = new HashSet<String>(all_accounts);
Set<String> set_cancel_accounts = new HashSet<String>(cancel_accounts);
set_all_account.retainAll(set_cancel_accounts);
set_all_account.forEach(x -> System.out.println("Element matched: "+x));

Or as said by kartiks in his comment you could call the retainAll method directly on the all_accounts array:

all_accounts.retainAll(cancel_accounts);
all_accounts.forEach(x -> System.out.println("matched element: "+x));

Pay attention with this solution because in this case retainAll applies directly on the ArrayList and modifies it (as you can see the final result is in the all_accounts array). Moreover duplicate elements remain in the result array.

Last implementation (if you want compute intersection and print the result all in one line, also this version keeps duplicate elements):

all_accounts.stream().filter(x -> cancel_accounts.contains(x)).forEach(x -> System.out.println("matched element: "+x)); 
user6904265
  • 1,938
  • 1
  • 16
  • 21
0

Just add an equals check to your for - loops (will work even without List#contains method)

for(String account: all_accounts) {
    System.out.println(account);

   for(String canceled: cancel_accounts){
     System.out.println(canceled);

     if(account.equals(cancelled)){
         //you've found first intersection, cancelled exists in both
         System.out.println(canceled + " is the same as " + account); 

     }
   }
}
C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47
  • I am trying like this , I still don't understand why it doesn't match: String found = null; for (String account: all_accounts) { for (String canceled: cancel_accounts) { System.out.println(canceled); found = canceled; } System.out.println(found); if(account.equals(found) ) { System.out.println(account); } } – Andre Nov 21 '16 at 23:15
  • Try it EXACTLY as the answer above - and watch where you put curly brackets. What you have pasted in the above comment has a serious logic mistake. – C0D3LIC1OU5 Nov 22 '16 at 07:40
  • I think the issue is that all the data is coming one element at a time, as in data sample above, thats why I can not find the match. – Andre Nov 22 '16 at 14:36
  • did you run the code above? If there are 2 same strings in those 2 arrays, code above will work. – C0D3LIC1OU5 Nov 22 '16 at 19:41
0

You can loop through the one list and search the second list for each element in first.

for (String account: all_accounts) {
    if (cancel_accounts.contains(account) {
        // Match found - do something....
        System.out.println(account);
    }
}
001
  • 13,291
  • 5
  • 35
  • 66
  • Just a note, if the value is of `primitive` type, `List#contains` method won't work. Should work with a `String` though. – C0D3LIC1OU5 Nov 21 '16 at 19:25