Skip to content

Instantly share code, notes, and snippets.

@levancho
Created May 17, 2023 19:28
Show Gist options
  • Save levancho/2e425d4dd3fd7d8d859f0340b96a4864 to your computer and use it in GitHub Desktop.
Save levancho/2e425d4dd3fd7d8d859f0340b96a4864 to your computer and use it in GitHub Desktop.
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