Skip to content

Instantly share code, notes, and snippets.

@milingo
Last active August 29, 2015 14:14
Show Gist options
  • Save milingo/d90d962d8ec81d095c5a to your computer and use it in GitHub Desktop.
Save milingo/d90d962d8ec81d095c5a to your computer and use it in GitHub Desktop.
# Turn a List<> into String[]
String[] strings = list.stream().toArray(String[]::new);
# Reduce by collecting over a field
list.stream().collect(Collectors.mapping(
Person::getName,
Collectors.toList())),
# Run several tasks asynchronously and wait for them
CompletableFuture<Map<String, List<String>>> campaignsByGeohashWithLocationsFuture =
CompletableFuture.supplyAsync(() -> findCampaignIdsInCoordinates(request));
CompletableFuture<Map<String, List<String>>> campaignByUserWithLocationsFuture =
CompletableFuture.supplyAsync(() -> findCampaignsIdsForUser(request));
Map<String, List<String>> campaignsByGeohashWithLocations = waitForResult(campaignsByGeohashWithLocationsFuture);
Map<String, List<String>> campaignByUserWithLocations = waitForResult(campaignByUserWithLocationsFuture);
private Map<String, List<String>> waitForResult(CompletableFuture<Map<String, List<String>>> campaignsWithLocationsFuture) {
Map<String, List<String>> campaignsWithLocations;
try {
campaignsWithLocations = campaignsWithLocationsFuture.get();
} catch (Exception e) {
campaignsWithLocations = new HashMap<>();
}
return campaignsWithLocations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment