0

I have a member function that will retrieve all membershipId of a member(one member might have multiples of membershipId).After retrieve all membershipId using List,it will call the url like this.

This is my service:

RestRequest request = RestRequest.newBuilder()
                .url("/membership/" + membershipId + "/outlet")
                .get();

This is my controller:

 @RequestMapping(value = "/favouriteStores", method = RequestMethod.GET)
        public Object FavouriteStores(ModelMap modelMap,HttpSession session)throws  Exception {
          String memberId = "5677a7075e3f1b998fc7483b";


 List<Membership> membershipList= memberService.getMembershipByMemberId(memberId);

 List<String>  membershipIds = membershipList.stream().map(m->m.getId()).collect(Collectors.toList());
         String membershipId = membershipIds.toString();

        Set<Outlet> outletSet = membershipService.getOutletByMembershipId(membershipId);

My problem is it will transform the whole membershipId in one url like this "membership/[12345, 54321]/outlet" It should be two url like "membership/[12345]/outlet" and "membership/[54321]/outlet"

I know we can use foreach to do that in controller,but i don't know how.Thanks for any helps.

2
  • 1
    First URL will be "membership/[12345]/outlet" or "membership/12345/outlet" ? Commented Jan 26, 2016 at 9:35
  • Hi,It should be "membership/12345/outlet" Commented Jan 26, 2016 at 9:50

1 Answer 1

3

Try map method of Stream instead : You can achieve this using map method of Stream.

Set<Outlet> outletSet = membershipIds.stream()
    .map(membershipService::getOutletByMembershipId)
    .collect(Collectors.toSet());

Even you can combine your previous stream operations and omit creation of intermediate list objects :

String memberId = "5677a7075e3f1b998fc7483b";

Set<Outlet> outletSet = memberService.getMembershipByMemberId(memberId)
    .stream()
    .map(Membership::getId)
    .map(membershipService::getOutletByMembershipId)
    .collect(Collectors.toSet())
Sign up to request clarification or add additional context in comments.

2 Comments

It would be even better, to combine this with the first stream operation to get rid of those intermediate lists which are counteracting the purpose of streams.
Right. I was too focused on the area of question. I have updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.