Cannot understand why this method is unresolved
Andrew Mclaughlin
I am studying Spring with Spring In Action book, i have the code from the book where it works fine:
@Slf4j
@Controller
@RequestMapping("/design")
public class DesignTacoController { @GetMapping public String showDesignForm(Model model) { List<Ingredient> ingredients = Arrays.asList( new Ingredient("FLTO", "Flour Tortilla", Type.WRAP), new Ingredient("COTO", "Corn Tortilla", Type.WRAP), new Ingredient("GRBF", "Ground Beef", Type.PROTEIN), ); Type[] types = Ingredient.Type.values(); for (Type type : types) { model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type)); } model.addAttribute("design", new Taco()); return "design"; }
} but when i typed it in my IDEA it says that method filterByType cannot be resolved, but there is no such problem in the book, and no any comment about this problem. I am new in the Spring, tried google a lot, but couldn't find any information about this problem and it source. Could you help me with this problem, a cannot move further because of it.screenshot from IDEA
52 Answers
It looks like this book just contains an error, it does not list the filterByType() method. It's not a Spring method. Here you go:
private List<Ingredient> filterByType(List<Ingredient> ingredients, Type type) { return ingredients.stream() .filter(x -> x.getType().equals(type)) .collect(Collectors.toList());
}Source: Manning Publications
2If you do not prefer readability:
Arrays.stream(types).forEach(a -> model.addAttribute(a.toString().toLowerCase(Locale.ROOT), ingredients.stream().filter(i -> a.equals(i.getType())).collect(Collectors.toList())));