Created
May 17, 2023 19:28
-
-
Save levancho/2e425d4dd3fd7d8d859f0340b96a4864 to your computer and use it in GitHub Desktop.
This file contains 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
List<MyObject> largeList = /* your large list */; | |
List<MyObject> smallList = /* your small list */; | |
Collections.sort(largeList, Comparator.comparing(MyObject::getTicker)); | |
List<MyObject> matchingObjects = smallList.stream() | |
.filter(item -> Collections.binarySearch(largeList, item, Comparator.comparing(MyObject::getTicker)) >= 0) | |
.collect(Collectors.toList()); | |
or | |
List<MyObject> largeList = /* your large list */; | |
List<MyObject> smallList = /* your small list */; | |
Set<String> tickerSet = largeList.stream() | |
.map(MyObject::getTicker) | |
.collect(Collectors.toSet()); | |
List<MyObject> matchingObjects = new ArrayList<>(); | |
List<MyObject> nonMatchingObjects = new ArrayList<>(); | |
for (MyObject item : smallList) { | |
if (tickerSet.contains(item.getTicker())) { | |
matchingObjects.add(item); | |
} else { | |
nonMatchingObjects.add(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment