Skip to content

Instantly share code, notes, and snippets.

@VenkataRaju
Last active March 11, 2018 22:45
Show Gist options
  • Save VenkataRaju/e92e1774a52b83636755f2946c82a62e to your computer and use it in GitHub Desktop.
Save VenkataRaju/e92e1774a52b83636755f2946c82a62e to your computer and use it in GitHub Desktop.
Java 8 collectors GroupingBy example
package a.b.c;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public class Java8CollectorsGroupingByExample
{
public static void main(String[] args)
{
List<Colony> colonies = Arrays.asList(new Colony(Arrays.asList(
new Family(Arrays.asList(new Person("Father01", 30), new Person("Mother01", 29), new Person("Son0101", 5),
new Person("Daughter0101", 3))),
new Family(Arrays.asList(new Person("Father02", 32), new Person("Mother02", 22), new Person("Son0201", 4),
new Person("Daughter0201", 1))))));
Map<IntRange, List<String>> personsByAge = colonies.stream().map(Colony::getFamilies).flatMap(Collection::stream)
.map(Family::getMembers).flatMap(Collection::stream)
.collect(Collectors.groupingBy(person -> rangeFor(person.getAge(), 2),
() -> new TreeMap<>(Comparator.<IntRange>reverseOrder()),
Collectors.mapping(Person::getName, Collectors.toList())));
personsByAge.forEach(
(range, persons) -> System.out.printf("%02d-%02d %s%n", range.getMin(), range.getMax(), persons));
// Output:
// 31-32 [Father02]
// 29-30 [Father01, Mother01]
// 21-22 [Mother02]
// 05-06 [Son0101]
// 03-04 [Daughter0101, Son0201]
// 01-02 [Daughter0201]
}
static IntRange rangeFor(int num, int blockSize)
{
int mul = num / blockSize;
int point = mul * blockSize;
return num % blockSize == 0 ? new IntRange(point - blockSize + 1, point)
: new IntRange(point + 1, point + blockSize);
}
static class IntRange implements Comparable<IntRange>
{
final int min, max;
public IntRange(int min, int max)
{
this.min = min;
this.max = max;
}
public int getMin()
{
return min;
}
public int getMax()
{
return max;
}
@Override
public int compareTo(IntRange other)
{
return Comparator.comparingInt(IntRange::getMin).thenComparingInt(IntRange::getMax).compare(this, other);
}
@Override
public String toString()
{
return "IntRange [min=" + min + ", max=" + max + "]";
}
}
static class Person
{
final String name;
final int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
@Override
public String toString()
{
return "Person [name=" + name + ", age=" + age + "]";
}
}
static class Family
{
final Collection<Person> members;
public Family(Collection<Person> members)
{
this.members = members;
}
public Collection<Person> getMembers()
{
return members;
}
}
static class Colony
{
final Collection<Family> families;
public Colony(Collection<Family> families)
{
this.families = families;
}
public Collection<Family> getFamilies()
{
return families;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment