Skip to content

Instantly share code, notes, and snippets.

@dhananjay-ng
Created May 5, 2020 14:03
Show Gist options
  • Save dhananjay-ng/f658441ddf95f12ba4f4cfa4d36641e9 to your computer and use it in GitHub Desktop.
Save dhananjay-ng/f658441ddf95f12ba4f4cfa4d36641e9 to your computer and use it in GitHub Desktop.
public static int totalAssetValues(final List<Asset> assets,
final Predicate<Asset> assetSelector) {
return assets.stream()
.filter(assetSelector)
.mapToInt(Asset::getValue)
.sum();
}
public static void main(final String[] args) {
List<Asset> assets = Arrays.asList(
new Asset(Asset.AssetType.BOND, 1000),
new Asset(Asset.AssetType.BOND, 2000),
new Asset(Asset.AssetType.STOCK, 3000),
new Asset(Asset.AssetType.STOCK, 4000)
);
System.out.println("Total of all assets: " +
totalAssetValues(assets, asset -> true));
System.out.println("Total of bonds: " +
totalAssetValues(assets, asset -> asset.getType() == AssetType.BOND));
System.out.println("Total of stocks: " +
totalAssetValues(assets, asset -> asset.getType() == AssetType.STOCK));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment