Created
September 17, 2013 13:14
-
-
Save vshank77/6594147 to your computer and use it in GitHub Desktop.
Efficient comparison of two lists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private static void compare(List<String> list1, List<String> list2) { | |
| Collections.sort(list1); | |
| Collections.sort(list2); | |
| System.out.println(list1.size()); | |
| System.out.println(list2.size()); | |
| List<String> listEx1 = Lists.newArrayList(); | |
| List<String> listEx2 = Lists.newArrayList(); | |
| Iterator<String> it1 = list1.iterator(); | |
| Iterator<String> it2 = list2.iterator(); | |
| String leftObj = null; | |
| String rightObj = null; | |
| while(it1.hasNext() || it2.hasNext()) { | |
| if(leftObj == null && it1.hasNext()) { | |
| leftObj = it1.next(); | |
| } | |
| if(rightObj == null && it2.hasNext()) { | |
| rightObj = it2.next(); | |
| } | |
| if(leftObj == null) { | |
| listEx2.add(rightObj); | |
| rightObj = null; | |
| } else if (rightObj == null) { | |
| listEx1.add(leftObj); | |
| leftObj = null; | |
| } else { | |
| int compared = leftObj.compareTo(rightObj); | |
| if(compared < 0) { | |
| listEx1.add(leftObj); | |
| leftObj = null; | |
| } else if (compared > 0) { | |
| listEx2.add(rightObj); | |
| rightObj = null; | |
| } else { | |
| leftObj = null; | |
| rightObj = null; | |
| } | |
| } | |
| } | |
| System.out.println(listEx1); | |
| System.out.println(listEx2); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment