Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Ask Question
I am getting the below error for Java 8 code developed below. In this example, trying to join all the names of the Dish Name into a single variable. With the below code I got this
"The method collect(Collector<? super Dish,A,R>) in the type Stream<Dish> is not applicable for the arguments (Collector<CharSequence,capture#3-of ?,String>)"
.
Dish.java
@Builder
@Data
@AllArgsConstructor
public class Dish {
public enum Type { MEAT, FISH, OTHER }
private final String name;
private final boolean vegetarian;
private final int calories;
private final Type type;
public static final List<Dish> menu =
Arrays.asList( new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 400, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH));
Here is the main method
String shortMenu = Dish.menu.stream().map(Dish::getName).collect(joining());
System.out.println(shortMenu);
String shortMenu1 = Dish.menu.stream().collect(joining()); //line-3
You've got the Lombok annotation @Data
which automatically creates a toString()
method, so presumably you were expecting line-3 to work. The toString()
method is only called automatically when you add it to a string (i.e. a literal string or another variable declared as String). For other uses, you need to call toString()
explicitly. Therefore, line-3 should be:
String shortMenu1 = Dish.menu.stream().map(Dish::toString).collect(joining()); //line-3
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.