이 예제가 있습니다. 이 튜토리얼 http://www.baeldung.com/java-groupingby-collector에서 가져 왔습니다. 그러나 그룹화 된 여러 매개 변수를 얻는 방법은 설명하지 않습니다. 는 예를 들어 다음과 같은 코드는매개 변수 별 맵의 Java 그룹 값
Map<BlogPostType, Integer> likesPerType = posts.stream()
.collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));
이 코드는 타이틀을 얻을 좋아의 합계를 얻을.
Map<BlogPostType, String> postsPerType = posts.stream()
.collect(groupingBy(BlogPost::getType,
mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]"))));
어떻게 든이 두 문장을 하나로 결합 할 수 있습니까? 저자별로 그룹화해야합니다. 제목과 유형은 각각 쉼표로 구분 된 하나의 문자열이어야하며 좋아하는 것의 유형이어야합니다.
클래스 블로그 게시물
public class BlogPost {
String title;
String author;
String type;
int likes;
public BlogPost(String title, String author, String type, int likes) {
this.title = title;
this.author = author;
this.type = type;
this.likes = 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;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
@Override
public String toString() {
return "BlogPost{" +
"title='" + title + '\'' +
", author='" + author + '\'' +
", type=" + type +
", likes=" + likes +
'}';
}
}
Main.java
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<Integer, List<BlogPost>> map = new HashMap<>();
map.put(2017, Arrays.asList(
new BlogPost("title1", "author1", "Guide", 10),
new BlogPost("title2", "author1", "News", 15),
new BlogPost("title3", "author2", "News", 5),
new BlogPost("title4", "author2", "Review", 10)
));
map.put(2018, Arrays.asList(
new BlogPost("title5", "author1", "Guide", 3),
new BlogPost("title6", "author1", "News", 2),
new BlogPost("title7", "author2", "News", 1),
new BlogPost("title8", "author2", "Review", 11)
));
System.out.println(map);
}
}
{2017=[BlogPost{title='title1', author='author1', type=Guide, likes=10}, BlogPost{title='title2', author='author1', type=News, likes=15}, BlogPost{title='title3', author='author2', type=News, likes=5}, BlogPost{title='title4', author='author2', type=Review, likes=10}],2018=[BlogPost{title='title5', author='author1', type=Guide, likes=3}, BlogPost{title='title6', author='author1', type=News, likes=2}, BlogPost{title='title7', author='author2', type=News, likes=1}, BlogPost{title='title8', author='author2', type=Review, likes=11}]}
그래서 결과 결과는 다음과 같이해야합니다 :
,{2017=[BlogPost{title='title1, title2', author='author1', type='Guide,News', likes=25}, BlogPost{title='title3, title4' author='author2', type='News, Review', likes=15}],2018=[BlogPost{title='title5, title6', author='author1', type='Guide,News', likes=5}, BlogPost{title='title7, title8', author='author2', type='News, Review', likes=12}]}
자바 8 람다로 할 수있을 때 더 좋습니다.
SQL 문이면. 이
SELECT author, title, type, SUM(likes) FROM BlogPost GROUP BY author;
을 수행 할 수 있습니다 방법 : 아마 그것은 같은 것?
그리고 귀하의 질문은 무엇입니까? – shmosel
그리고 귀하의 질문은 무엇입니까? 시도해 봤어? 연구를 마쳤습니까? Java로 컬렉션의 요소를 그룹화하는 방법에 관한 수십 가지 질문이 있습니다. 읽었 니? –
예. 나는 좋은 자습서를 발견했다. http://www.baeldung.com/java-groupingby-collector https://www.mkyong.com/java8/java-8-collectors-groupingby-and-mapping-example/ –