- Notifications
You must be signed in to change notification settings - Fork0
namvdo/lambdas-and-streams-best-practices
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Lambda and Stream best practices extracted from the Modern Java in Action - Lambda, streams, functional and reactive programming and some others. Also, this repo includes some tips when using lambda and streams as a Java developer.
Operation | Type | Return Type | Argument of the Operation | Function descriptor |
---|---|---|---|---|
filter | Intermediate | Stream<T> | Predicate<T> | T -> boolean |
map | Intermediate | Stream<R> | Function<T, R> | T -> R |
limit | Intermediate | Stream<T> | long | |
distinct | Intermediate | Stream<T> |
Operation | Type | Return Type | Purpose |
---|---|---|---|
forEach | Terminal | void | Consumes each element from a stream and applies a lambda to each of them. |
count | Terminal | long | Returns the number of elements in a stream. |
collect | Terminal | Stream<T> | Returns a collection from a stream (such asMap ,List or eventInteger ). |
List<String>highCaloricDishes =newArrayList<>();Iterator<String>iterator =menu.iterator();while(iterator.hasNext()) {Dishdish =iterator.next();if(dish.getCalories() >300) {highCaloricDishes.add(d.getName()); }}
List<String>highCaloricDish =menu.stream() .filter(dish ->dish.getCalories() >300) .collect(toList());
Iterator<String>iterator =menu.iterator();while (iterator.hasNext()) {Dishdish =iterator.next();System.out.println(dish);}
menu.forEach(System.out::println);
Set<Dish>dishes =newHashSet<>();intcount =0;for(Dishdish :menu) {if (dish.getCalories() >300) {dishes.add(dish);count++; }if (count ==3)break;}
longcount =menu.stream() .filter(dish ->dish.getCalories() >300) .distinct() .limit(3) .count();
List<Dish>vegetarianDishes =newArrayList<>();for(Dishd:menu) {if(d.isVegetarian()){vegetarianDishes.add(d); }}
List<Dish>vegetarianDishes =menu.stream() .filter(Dish::isVegetarian) .collect(toList());
List<Dish>dishesWithLowerThan320Calories =newArrayList<>();for(Dishdish :menu) {if (dish.getCalories() <320) {dishesWithLowerThan320Calories.add(dish); }}
List<Dish>slicedMenu =specialMenu.stream() .takeWhile(dish ->dish.getCalories() <320) .collect(toList());
Get all the dishes with the calories greater than or equal to 320 (presume the collection is already sorted)
List<Dish>dishesWithLowerThan320Calories =newArrayList<>();for(Dishdish :menu) {if (dish.getCalories() >=320) {dishesWithLowerThan320Calories.add(dish); }}
List<Dish>slicedMenu =specialMenu.stream() .dropWhile(dish ->dish.getCalories() <320) .collect(toList());
List<Dish>dishes =newArrayList<>();intcount =0;for(Dishdish :menu) {if (dish.getCalories() >300) {dishes.add(dish); }if (count ==3) {break; }}
List<Dish>dishes =specialMenu .stream() .filter(dish ->dish.getCalories() >300) .limit(3) .collect(toList());
List<Dish>dishes =newArrayList<>();intskipCount =0;for(Dishdish :menu) {if (dish.getCalories() >300 &&skipCount ==3) {dishes.add(dish); }if (skipCount <3) {skipCount++; }}
List<Dish>dishes =menu.stream() .filter(dish ->dish.getCalories() >300) .skip(3) .collect(Collectors.toList());
List<Dish>dishes =newArrayList<>();intcount =0;for(Dishdish :menu) {if (dish.getType() ==Dish.Type.MEAT) {dishes.add(dish);count++; }if (count ==2) {break; }}
List<Dish>dishes =menu.stream() .filter(dish ->dish.getType() ==Dish.Type.MEAT) .limit(2) .collect(Collectors.toList());
Map<Dish.Type,List<Dish>>typeToDish =newHashMap<>();for(Dishdish :menu) {List<Dish>dishes =typeToDish.get(dish.getType());if (dishes ==null) {dishes =newArrayList<>(); }dishes.add(dish);typeToDish.put(dish.getType(),dishes);}
Map<Dish.Type,List<Dish>>typeToDish =menu.stream().collect(groupingBy(Dish::getType));
About
Lambda and Stream best practices excerpted from the Modern Java in Action - Lambda, streams, functional and reactive programming.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published