Can I rewrite this code using Stream Api, what will it look like?

Set<ContactGroup> groups = user.getGroups(); Set<String> groupNames = new HashSet<>(); for (ContactGroup group : groups) { groupNames.add(group.getName()); } 
  • something like groups.stream().map(g->g.getName()).collect(Collectors.toSet()) - Grundy

1 answer 1

 Set<String> groupNames = user.getGroups() .stream() .map(ContactGroup::getName) .collect(Collectors.toSet());