0

How to avoid imperative loops and use the Java's Streaming API for the below mentioned implementation for the google directory API?

List<GroupMember> groupMemberList = new ArrayList<>();
Directory.Members.List listRequest = getDirectoryApi().members().list(groupEmailAddress);
Members members;
do {
    members = listRequest.execute();
    members.getMembers().forEach(member -> groupMemberList.add(new
        GroupMember(member.getId(), member.getEmail())));
} while (null != members.getNextPageToken());
return groupMemberList;
ThisaruG
  • 3,222
  • 7
  • 38
  • 60
  • 3
    Streams are not a good usecase for pagination – Thiyagu Jan 12 '23 at 05:47
  • 6
    Avoiding imperative loops is not a sensible goal. The goal should be to maximize readability ... and some tasks are not more readable (or efficient) if you try to shoe-horn them into a solution using the Stream's API. Just because you have bought a new hammer doesn't mean you should use it for *all* of your woodworking tasks. – Stephen C Jan 12 '23 at 06:23
  • 2
    Maybe I'm wrong about this ... but shouldn't your code be using `setNextPageToken()` to advance to the next page? As per https://stackoverflow.com/a/31260400/139985 ? – Stephen C Jan 12 '23 at 06:34
  • @StephenC You are right on this. Thanks for pointing that out. – Anjula Paulus Jan 12 '23 at 06:56

0 Answers0