Last active
September 20, 2016 07:52
-
-
Save sermojohn/a072a2feb63700f298d3350e6a222b8a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gr.iserm.java.javaplayground.lambdas; | |
import java.util.Arrays; | |
import java.util.List; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import java.util.stream.Collectors; | |
import static java.lang.System.out; | |
public class StreamGroupByExample { | |
public static void main(String[] args) { | |
String city = "Athens"; | |
//solution 1 - global state 'city' is associated with each item before iteration. | |
out.println("----Solution 1-----"); | |
List<Person1> person1List = Arrays.asList(new Person1("Giannis", "Frangos"), new Person1("Nikos", "Lianeris"), new Person1("Eleftherios", "Tsallas"), | |
new Person1("Panagis", "Lefteratos"), new Person1("Giannis", "Sermetziadis"), new Person1("Nikos", "Tester")); | |
person1List.stream() | |
.forEach(p -> p.setCity(city)); | |
person1List.stream() | |
.collect(Collectors.groupingBy(Person1::createKey)) | |
.entrySet().stream() | |
.forEach(out::println); | |
//solution 2 - using static key generation method. The keyGenerationFunction has no side-effects. | |
out.println("----Solution 2-----"); | |
List<Person2> person2List = Arrays.asList(new Person2("Giannis", "Frangos"), new Person2("Nikos", "Lianeris"), new Person2("Eleftherios", "Tsallas"), | |
new Person2("Panagis", "Lefteratos"), new Person2("Giannis", "Sermetziadis"), new Person2("Nikos", "Tester")); | |
Function<Person2, Person2.Person2Key> keyGenerationFunction = Person2.createKeyStaticFunction(city); | |
person2List.stream() | |
.collect(Collectors.groupingBy(keyGenerationFunction)) | |
.entrySet().stream() | |
.forEach(out::println); | |
//solution 3 - using instance key generation method. The supplier returned by createKeyInstanceFunction is created | |
// per item and has side-effects with person instance variables. | |
out.println("----Solution 3-----"); | |
person2List = Arrays.asList(new Person2("Giannis", "Frangos"), new Person2("Nikos", "Lianeris"), new Person2("Eleftherios", "Tsallas"), | |
new Person2("Panagis", "Lefteratos"), new Person2("Giannis", "Sermetziadis"), new Person2("Nikos", "Tester")); | |
person2List.stream() | |
.collect(Collectors.groupingBy(p -> p.createKeyInstanceFunction(city).get())) | |
.entrySet().stream() | |
.forEach(out::println); | |
} | |
private static class Person1 { | |
private String name, lastname; | |
private String city; | |
public Person1(String name, String lastname) { | |
this.name = name; | |
this.lastname = lastname; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getLastname() { | |
return this.lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
public String getCity() { | |
return this.city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public Person1Key createKey() { | |
return new Person1Key(getCity()+"_"+getName()); | |
} | |
@Override | |
public String toString() { | |
return "Person1{" + | |
"name='" + name + '\'' + | |
", lastname='" + lastname + '\''+ | |
'}'; | |
} | |
public static class Person1Key { | |
private String key; | |
public Person1Key(String key) { | |
this.key = key; | |
} | |
public String getKey() { | |
return this.key; | |
} | |
public void setKey(String key) { | |
this.key = key; | |
} | |
@Override | |
public String toString() { | |
return "Person1Key{" + | |
"key='" + key + '\'' + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || this.getClass() != o.getClass()) return false; | |
StreamGroupByExample.Person1.Person1Key that = (StreamGroupByExample.Person1.Person1Key) o; | |
return this.key != null ? this.key.equals(that.key) : that.key == null; | |
} | |
@Override | |
public int hashCode() { | |
return this.key != null ? this.key.hashCode() : 0; | |
} | |
} | |
} | |
private static class Person2 { | |
private String name, lastname; | |
public Person2(String name, String lastname) { | |
this.name = name; | |
this.lastname = lastname; | |
} | |
public String getName() { | |
return this.name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getLastname() { | |
return this.lastname; | |
} | |
public void setLastname(String lastname) { | |
this.lastname = lastname; | |
} | |
@Override | |
public String toString() { | |
return "Person2{" + | |
"name='" + name + '\'' + | |
", lastname='" + lastname + '\'' + | |
'}'; | |
} | |
public Supplier<Person2Key> createKeyInstanceFunction(String city) { | |
return () -> new Person2Key(city+"_"+getName()); | |
} | |
public static Function<Person2, Person2Key> createKeyStaticFunction(String city) { | |
return (p) -> new Person2Key(city+"_"+p.getName()); | |
} | |
public static class Person2Key { | |
private String key; | |
public Person2Key(String key) { | |
this.key = key; | |
} | |
public String getKey() { | |
return this.key; | |
} | |
public void setKey(String key) { | |
this.key = key; | |
} | |
@Override | |
public String toString() { | |
return "Person2Key{" + | |
"key='" + key + '\'' + | |
'}'; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || this.getClass() != o.getClass()) return false; | |
StreamGroupByExample.Person2.Person2Key that = (StreamGroupByExample.Person2.Person2Key) o; | |
return this.key != null ? this.key.equals(that.key) : that.key == null; | |
} | |
@Override | |
public int hashCode() { | |
return this.key != null ? this.key.hashCode() : 0; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment