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!

3
  • Possible duplicate of How can I test if an array contains a certain value? Commented Nov 21, 2016 at 19:20
  • @PeterPerháč The question is quite different, here the OP asks for array that contains more then one element not only one. Commented Nov 21, 2016 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 Commented Nov 21, 2016 at 21:59

4 Answers 4

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.

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

1 Comment

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

Comments

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

1 Comment

Just a note, if the value is of primitive type, List#contains method won't work. Should work with a String though.
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); } } } 

4 Comments

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); } }
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.
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.
did you run the code above? If there are 2 same strings in those 2 arrays, code above will work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.