import com.google.common.collect.Lists; import java.util.List; public class ListsPartitionExample { public static void main(String[] args) { Listnumbers = Lists.newArrayList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20); List > partitions = Lists.partition(numbers, 5); for (List
partition : partitions) { System.out.println(partition); } } }
[1, 2, 3, 4, 5] [6, 7, 8, 9, 10] [11, 12, 13, 14, 15] [16, 17, 18, 19, 20]
import com.google.common.collect.Lists; import java.util.List; public class ListsPartitionExample { public static void main(String[] args) { Listnames = Lists.newArrayList("John", "Mary", "Mark", "Emily", "David", "Karen", "Andy", "Olivia", "Peter", "Lisa"); List > partitions = Lists.partition(names, 3); for (List
partition : partitions) { System.out.println(partition); } } }
[John, Mary, Mark] [Emily, David, Karen] [Andy, Olivia, Peter] [Lisa]In this example, we created a list of names and used the Lists.partition method to divide it into partitions of size 3. The resulting partitions are printed to the console. The Lists.partition method is a part of the com.google.common.collect library.