Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rampatra/Algorithms-and-Data-Structures-in-Java
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c23f98b
Choose a base ref
...
head repository: HDumo/Algorithms-and-Data-Structures-in-Java
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2618693
Choose a head ref

There isn’t anything to compare.

rampatra:c23f98b and HDumo:2618693 are entirely different commit histories.

Showing with 64 additions and 0 deletions.
  1. +64 −0 src/me/ramswaroop/java8/FlatMapInStreams.java
64 changes: 64 additions & 0 deletions src/me/ramswaroop/java8/FlatMapInStreams.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package me.ramswaroop.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author ramswaroop
* @version 17/02/2017
*/
public class FlatMapInStreams {

public static long countTotalIngredientsInAllDishes(List<Dish> dishes) {
return dishes.stream().map(Dish::getIngredients).flatMap(List::stream).count();
}

public static void main(String[] a) {
List<String> ingredients = new ArrayList<>();
ingredients.add("rice");
ingredients.add("chicken");
ingredients.add("haldi");
List<Dish> dishes = Arrays.asList(
new Dish("biriyani", 600, ingredients),
new Dish("biriyani", 600, new ArrayList<>()));
// to show whether empty List is counted in flatMap
System.out.println(countTotalIngredientsInAllDishes(dishes));
}
}

class Dish {
private String name;
private int calories;
private List<String> ingredients;

public Dish(String name, int calories, List<String> ingredients) {
this.name = name;
this.calories = calories;
this.ingredients = ingredients;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getCalories() {
return calories;
}

public void setCalories(int calories) {
this.calories = calories;
}

public List<String> getIngredients() {
return ingredients;
}

public void setIngredients(List<String> ingredients) {
this.ingredients = ingredients;
}
}