There is such a method

public static List<Pair<String, String>> getCityAssignedCodeForProvider(Set<String> departureAssignedCodes, Set<String> arrivalAssignedCodes) { List<Pair<String, String>> pairList = new ArrayList<>(); for (String departure : departureAssignedCodes) { String[] splitDeparture = departure.split(","); for (String arrival : arrivalAssignedCodes) { String[] splitArrival = arrival.split(","); for (String departureSplitItem : splitDeparture) { for (String arrivalSplitItem : splitArrival) { pairList.add(new Pair<>(departureSplitItem, arrivalSplitItem)); } } } } return pairList; } 

Help please convert to lambda expression

  • What have you tried? and for what? code then worker - Vartlok
  • using stream, probably, not lambda. By the condition, you have two sets of strings, inside of which there are some codes separated by commas, do you need to return all pairs of codes? - zRrr
  • Yes, you need to return each one correctly. But in a line there can be either 1 code (without a comma) or 2 and 3 and 10 codes separated by commas. Therefore, each with each - Sacha Krasnyansky

1 answer 1

Probably too late, but for the sake of interest did:

  public static List<Pair<String, String>> getCityAssignedCodeForProvider(Set<String> departureAssignedCodes, Set<String> arrivalAssignedCodes) { return departureAssignedCodes .stream() .flatMap(dString -> Arrays.stream(dString.split(","))) .map( dCode -> arrivalAssignedCodes .stream() .flatMap(aString -> Arrays.stream(aString.split(","))) .map(aCode -> new Pair<>(dCode, aCode)) .collect(Collectors.toList())) .flatMap(Collection::stream) .collect(Collectors.toList()); } 
  • it is possible instead of .map( dCode -> arrivalAssignedCodes use right flatMap and get rid of collect - zRrr