使用Java 8映射和maxBy操作时无法解决错误

时间:2017-06-02 22:50:55

标签: 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 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)))));

我无法弄清楚如何解决这个问题。 IDE将以下行指向红色

 mapping(BlogPost::getTitles

以下提到的错误/标记显示我无法解决。

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
}

1 个答案:

答案 0 :(得分:3)

BlogPost::getTitles以红色突出显示,因为它是一个拼写错误,您的方法称为getTitle。编译器的消息“类型BlogPost没有定义适用于此的getTitles(T)”告诉你到底出了什么问题。

此外,postsPer的类型应为Map<BlogPostType...而不是Map<String, ...

有了这个,

Map<BlogPostType, Optional<String>> postsPer = posts.stream().collect(
        Collectors.groupingBy(BlogPost::getType,
            Collectors.mapping(BlogPost::getTitle,
                Collectors.maxBy(Comparator.comparing(String::length))))); 

应该编译好。

另一方面,您可以避免Optional,并使用3参数toMap收集器直接表达:

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))
        ));