daveb
daveb

Reputation: 3535

How do you partition sequential point data in Java into equal size groups

I would like to know a way of partitioning a set of sequential points into equal sized groups of points (groups have fixed size) e.g for instance group size could be 4 where each group would have 4 points

I am then looking to process points in each group a group at a time

My aim is to eventually remove a point from each group based on a constraint

Also any idea of which data structure would be best recommended for the partitioning task

Upvotes: 0

Views: 108

Answers (1)

Andy Turner
Andy Turner

Reputation: 140494

You can partition into a list-of-lists by using List.subList:

List<List<T>> listOfLists = new ArrayList<>();
for (int i = 0; i < list.size(); i += partitionSize) {
  listOfLists.add(list.subList(i, Math.min(i + partitionSize, list.size());
}

Note that the elements of listOfLists are views into list.

Upvotes: 1

Related Questions