I think you can get rid segmentMaxId instance variable, and replace it with segmentToId.size()