[BAEL-3292] Add missing code snippets from the Java 8 groupingBy article

The Tuple had to be changed because it required an equals/hashcode

Also: formatted with eclipse profile
This commit is contained in:
Martin van Wingerden
2019-10-23 12:04:31 +02:00
parent bcf119bf1a
commit a42fc708fb
2 changed files with 58 additions and 19 deletions

View File

@@ -1,12 +1,12 @@
package com.baeldung.java_8_features.groupingby;
public class Tuple {
import java.util.Objects;
public class Tuple {
private final BlogPostType type;
private final String author;
private BlogPostType type;
private String author;
public Tuple(BlogPostType type, String author) {
super();
this.type = type;
this.author = author;
}
@@ -15,20 +15,27 @@ public class Tuple {
return type;
}
public void setType(BlogPostType type) {
this.type = type;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Tuple tuple = (Tuple) o;
return type == tuple.type && author.equals(tuple.author);
}
@Override
public int hashCode() {
return Objects.hash(type, author);
}
@Override
public String toString() {
return "Tuple [type=" + type + ", author=" + author + ", getType()=" + getType() + ", getAuthor()=" + getAuthor() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
}
}