I have two lists of phone numbers. 1st list is a subset of 2nd list. I ran two different algorithms below to determine which phone numbers are contained in both of two lists.
- Way 1:
- Sortting 1st list: Arrays.sort(FirstList);
- Looping 2nd list to find matched element: If Arrays.binarySearch(FistList, 'each of 2nd list') then OK
- Sortting 1st list: Arrays.sort(FirstList);
- Way 2:
- Convert 1st list into HashMap with key/valus is ('each of 1st list', Boolean.TRUE)
- Looping 2nd list to find matched element: If FirstList.containsKey('each of 2nd list') then OK
- Convert 1st list into HashMap with key/valus is ('each of 1st list', Boolean.TRUE)
It results in Way 2 ran within 5 seconds is faster considerably than Way 1 with 39 seconds. I can't understand the reason why.
I appreciate your any comments.