# The problem – grouping continuous integers

A while back, I wanted a simple and elegant way to solve the following problem:

Given: A sorted list of numbers

When: We group these numbers

Then: Each number in a group is higher by one than its predecessor. So, let's be good and write a few tests first:

` @Test public void testFourGroup() { List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5, 9, 11, 20, 21, 22); List<List<Integer>> groups = groupThem.groupThem(list); assertThat(groups.size(), equalTo(4)); assertThat(groups.get(0), contains(1, 2, 3, 4, 5)); assertThat(groups.get(1), contains(9)); assertThat(groups.get(2), contains(11)); assertThat(groups.get(3), contains(20, 21, 22)); } @Test public void testNoGroup() { List<Integer> emptyList = Lists.newArrayList(); ...`

Get Scala Functional Programming Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.