标签:bool creat lis ... other cto color app final
1 import static java.util.Comparator.comparing; 2 import static java.util.stream.Collectors.toList; 3 4 import java.util.ArrayList; 5 import java.util.Collections; 6 import java.util.Comparator; 7 import java.util.List; 8 9 /** 10 * 在这里编写类的功能描述 11 * 12 * @author shangjinyu 13 * @created 2017/10/4 14 */ 15 public class Notes { 16 //和迭代器类似,流只能遍历一次 17 18 public static List<String> getLowCaloricDishesNamesInJava7(List<Dish> dishes){ 19 List<Dish> lowCaloricDishes = new ArrayList<>(); 20 for(Dish d: dishes){ 21 if(d.getCalories() > 400){ 22 lowCaloricDishes.add(d); 23 } 24 } 25 List<String> lowCaloricDishesName = new ArrayList<>(); 26 Collections.sort(lowCaloricDishes, new Comparator<Dish>() { 27 public int compare(Dish d1, Dish d2){ 28 return Integer.compare(d1.getCalories(), d2.getCalories()); 29 } 30 }); 31 for(Dish d: lowCaloricDishes){ 32 lowCaloricDishesName.add(d.getName()); 33 } 34 return lowCaloricDishesName; 35 } 36 37 public static List<String> getLowCaloricDishesNamesInJava8(List<Dish> dishes){ 38 return dishes.stream() 39 .filter(d -> d.getCalories() > 400) 40 .sorted(comparing(Dish::getCalories)) 41 .map(Dish::getName) 42 .collect(toList()); 43 } 44 public static List<String> getLowCaloricDishesNamesInJava8withLambda1(List<Dish> dishes){ 45 return dishes.stream() 46 .filter(d -> d.getCalories() > 400) 47 .sorted(Comparator.comparing(Dish::getCalories)) 48 .map(a -> a.getName()) 49 .collect(toList()); 50 } 51 public static List<String> getLowCaloricDishesNamesInJava8withLambda2(List<Dish> dishes){ 52 return dishes.stream() 53 .filter(d -> d.getCalories() > 400) 54 .sorted((a,b) -> Integer.compare(a.getCalories(), b.getCalories())) 55 .map(a -> a.getName()) 56 .collect(toList()); 57 } 58 public static List<String> names = Dish.menu.stream() 59 .filter(d -> { 60 System.out.println("filtering " + d.getName()); 61 return d.getCalories() > 300; 62 }).map(d -> { 63 System.out.println("mapping " + d.getName()); 64 return d.getName(); 65 }).limit(3) 66 .collect(toList()); 67 /** 68 * System.out.println(names); 69 * filtering pork 70 * mapping pork 71 * filtering beef 72 * mapping beef 73 * filtering chicken 74 * mapping chicken 75 * [pork, beef, chicken] 76 */ 77 public static void main(String...args){ 78 System.out.println("---"); 79 // Java 7 80 getLowCaloricDishesNamesInJava7(Dish.menu).forEach(System.out::println); 81 System.out.println("---"); 82 // Java 8 83 getLowCaloricDishesNamesInJava8(Dish.menu).forEach(System.out::println); 84 System.out.println(names); 85 } 86 }
1 import java.util.*; 2 3 public class Dish { 4 5 private final String name; 6 private final boolean vegetarian; 7 private final int calories; 8 private final Type type; 9 10 public Dish(String name, boolean vegetarian, int calories, Type type) { 11 this.name = name; 12 this.vegetarian = vegetarian; 13 this.calories = calories; 14 this.type = type; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public boolean isVegetarian() { 22 return vegetarian; 23 } 24 25 public int getCalories() { 26 return calories; 27 } 28 29 public Type getType() { 30 return type; 31 } 32 33 public enum Type { MEAT, FISH, OTHER } 34 35 @Override 36 public String toString() { 37 return name; 38 } 39 40 public static final List<Dish> menu = 41 Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT), 42 new Dish("beef", false, 700, Dish.Type.MEAT), 43 new Dish("chicken", false, 400, Dish.Type.MEAT), 44 new Dish("french fries", true, 530, Dish.Type.OTHER), 45 new Dish("rice", true, 350, Dish.Type.OTHER), 46 new Dish("season fruit", true, 120, Dish.Type.OTHER), 47 new Dish("pizza", true, 550, Dish.Type.OTHER), 48 new Dish("prawns", false, 400, Dish.Type.FISH), 49 new Dish("salmon", false, 450, Dish.Type.FISH)); 50 }
标签:bool creat lis ... other cto color app final
原文地址:http://www.cnblogs.com/3013218061shang/p/7625487.html