Java 8 使用Java 8映射和maxBy操作时无法解决此错误
这就是我想要达到的目标 按类型对博客文章进行分组。在每种类型中,尝试生成标题的名称并减少最大长度 这就是我试图使用的逻辑Java 8 使用Java 8映射和maxBy操作时无法解决此错误,java-8,Java 8,这就是我想要达到的目标 按类型对博客文章进行分组。在每种类型中,尝试生成标题的名称并减少最大长度 这就是我试图使用的逻辑 BlogPost b1 = new BlogPost("Story behind Harry Potter", "J.K.Rowling", BlogPostType.FICTION, 100); BlogPost b2= new BlogPost("Java 8 Tutorial", "Vinay", BlogPostType.TECH, 10);
BlogPost b1 = new BlogPost("Story behind Harry Potter", "J.K.Rowling", BlogPostType.FICTION, 100);
BlogPost b2= new BlogPost("Java 8 Tutorial", "Vinay", BlogPostType.TECH, 10);
BlogPost b3 = new BlogPost("Python Tutorial", "Jim", BlogPostType.TECH, 20);
BlogPost b4 = new BlogPost("Mission Impossible", "Kim", BlogPostType.REVIEW, 40);
BlogPost b5 = new BlogPost("Bomb Blast", "Kenny", BlogPostType.NEWS, 200);
BlogPost b6 = new BlogPost("President Visits", "Laura", BlogPostType.NEWS, 400);
List<BlogPost> posts = Arrays.asList(b1,b2,b3,b4,b5,b6);
Map<String, Optional<String>> postsPer = posts.stream().collect(
Collectors.groupingBy(BlogPost::getType,
Collectors.mapping(BlogPost::getTitles,
Collectors.maxBy(Comparator.comparing(String::length)))));
下面提到的错误/标记显示了我无法解决的问题
Multiple markers at this line
- The method mapping(Function<? super T,? extends U>, Collector<? super U,A,R>) in the type Collectors is not applicable for the arguments (BlogPost::getTitles, Collector<String,capture#60-
of ?,Optional<String>>)
- The type BlogPost does not define getTitles(T) that is applicable here
BlogPost::getTitles
以红色突出显示,因为它是一个打字错误,您的方法称为getTitle
。编译器的消息“BlogPost类型没有定义适用于此处的getTitles(T)”准确地告诉您出了什么问题
另外,
postsPer
的类型应该是MapI不完全理解这里发生了什么,但是如果我不得不猜测,我会说BlogPost::getTitles
不是函数类型,我感到很尴尬。我盯着代码看了45分钟,思考出了什么问题,但一直都是打字错误。非常感谢您耐心地指出。谢谢你,米莎。
Multiple markers at this line
- The method mapping(Function<? super T,? extends U>, Collector<? super U,A,R>) in the type Collectors is not applicable for the arguments (BlogPost::getTitles, Collector<String,capture#60-
of ?,Optional<String>>)
- The type BlogPost does not define getTitles(T) that is applicable here
package com.main.java8.streams.groupingby;
class BlogPost {
String title;
String author;
BlogPostType type;
int likes;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
/*@Override
public String toString() {
return "BlogPost [title=" + title + ", author=" + author + ", type=" + type + ", likes=" + likes + "]";
}*/
public BlogPostType getType() {
return type;
}
@Override
public String toString() {
return "BlogPost [title=" + title + "]";
}
public void setType(BlogPostType type) {
this.type = type;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
public BlogPost(String title, String author, BlogPostType type, int likes) {
super();
this.title = title;
this.author = author;
this.type = type;
this.likes = likes;
}
}
package com.main.java8.streams.groupingby;
enum BlogPostType {
NEWS,
REVIEW,
GUIDE,
FICTION,
TECH
}
Map<BlogPostType, Optional<String>> postsPer = posts.stream().collect(
Collectors.groupingBy(BlogPost::getType,
Collectors.mapping(BlogPost::getTitle,
Collectors.maxBy(Comparator.comparing(String::length)))));
import static java.util.stream.Collectors.toMap;
import static java.util.Comparator.comparing;
Map<BlogPostType, String> postsPer = posts.stream()
.collect(toMap(
BlogPost::getType,
BlogPost::getTitle,
BinaryOperator.maxBy(comparing(String::length))
));