Middle+
// list of car license plates, numbers are always standard of one type // need to output each number separately to the console // then create a map of number to its digits (example A123BC:123) where key is A123BC and value is 123 //var autoNumbers = List.of(List.of("A123BC", "B421FT"), // List.of("O543EC","B421FT", "I222OA"))
sobes.tech AI
Answer from AI
java import java.util.; import java.util.stream.;
public class AutoNumbersExample { public static void main(String[] args) { var autoNumbers = List.of( List.of("A123BC", "B421FT"), List.of("O543EC", "B421FT", "I222OA") );
// Print each number separately
autoNumbers.stream()
.flatMap(Collection::stream)
.forEach(System.out::println);
// Create a map from number to digits
Map<String, String> numberToDigits = autoNumbers.stream()
.flatMap(Collection::stream)
.distinct() // if unique numbers are needed
.collect(Collectors.toMap(
number -> number,
number -> number.replaceAll("[^0-9]", "")
));
System.out.println(numberToDigits);
}
}
Here we use `flatMap` to transform a list of lists into a stream of strings, print them, and then collect into a map where the key is the number and the value is only the digits from the number.