暮色呼如
恐怕您的问题对于“哪个服务器不相同”问题并不完全清楚。我不认为它定义得很好。例如,如果您有四台服务器,每台服务器都有不同的作业版本怎么办?考虑到这一点,我假设您想要的是通过以下测试:import static org.junit.jupiter.api.Assertions.assertEquals;import java.util.List;import java.util.Map;import java.util.Set;import org.junit.jupiter.api.Test;class QuestionTest { @Test void example() { var input = List.of( new Version("dev-1.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.1"), new Job("c", "2.0.1") )), new Version("dev-2.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.1"), new Job("c", "2.0.1") )), new Version("dev-3.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.1"), new Job("c", "2.0.2") )), new Version("uk-1.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.0"), new Job("c", "2.0.2") )), new Version("uk-2.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.0"), new Job("c", "2.0.2") )), new Version("uk-3.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.0"), new Job("c", "2.0.2") )), new Version("uk-4.lan", List.of( new Job("a", "1.1.1"), new Job("b", "10.0.1"), new Job("c", "2.0.2") )) ); var expectedOutput = Map.of( "a", Map.of( "DEV", Map.of( "1.1.1", Set.of(1, 2, 3) ), "UK", Map.of( "1.1.1", Set.of(1, 2, 3, 4) ) ), "b", Map.of( "DEV", Map.of( "10.0.1", Set.of(1, 2, 3) ), "UK", Map.of( "10.0.0", Set.of(1, 2, 3), "10.0.1", Set.of(4) ) ), "c", Map.of( "DEV", Map.of( "2.0.1", Set.of(1, 2), "2.0.2", Set.of(3) ), "UK", Map.of( "2.0.2", Set.of(1, 2, 3, 4) ) ) ); var actualOutput = Main.parse(input); assertEquals(expectedOutput, actualOutput); }}有了这个假设,您的问题就变成了使用 Java lambda 的有趣练习。我找到的解决方案是:import static java.util.stream.Collectors.groupingBy;import static java.util.stream.Collectors.mapping;import static java.util.stream.Collectors.toSet;import java.util.List;import java.util.Map;import java.util.Set;class Main { static Map<String, Map<String, Map<String, Set<Integer>>>> parse(List<Version> input) { return input.stream() .flatMap(version -> version.jobs.stream().map(job -> new Entry(version, job))) .collect( groupingBy( entry -> entry.jobName, groupingBy( entry -> entry.serverName, groupingBy( entry -> entry.jobVersion, mapping(entry -> entry.serverNumber, toSet()) ) ) ) ); }}助手类Entry定义为:class Entry { final String serverName; final int serverNumber; final String jobName; final String jobVersion; Entry(Version version, Job job) { this.serverName = version.serverName; this.serverNumber = version.serverNumber; this.jobName = job.name; this.jobVersion = job.version; }}与:class Job { final String name; final String version; Job(String name, String version) { this.name = name; this.version = version; }}最后:import java.util.List;import java.util.Locale;import java.util.regex.Matcher;import java.util.regex.Pattern;class Version { private static final Pattern SERVER_NAME_PATTERN = Pattern.compile("(\\w+)-(\\d+)\\.lan"); final String server; final List<Job> jobs; final String serverName; final int serverNumber; Version(String server, List<Job> jobs) { this.server = server; this.jobs = List.copyOf(jobs); Matcher matcher = SERVER_NAME_PATTERN.matcher(server); if (matcher.matches()) { this.serverName = matcher.group(1).toUpperCase(Locale.US); this.serverNumber = Integer.parseInt(matcher.group(2)); } else { throw new IllegalArgumentException("Invalid server: " + server); } }}