猿问

将 Java 类转换为 List<List<String>> 以显示在表格中

我想在 Java 中的某些类之间进行转换以显示在表格中。我收到一个List<Version>(删除了 getter/setter 以使其更短):


public class Version {

    private String server;

    private List<Job> jobs;

}

public class Job {

    private String name;

    private String version;

}

现在,我所拥有的是一个工具来检查所有作业的服务器数据: - 列表中的每一项都是一个服务器的信息,因此多个服务器包含多个作业,但每个服务器都有相同的作业,但版本可能不同。- 所以,Version.server是服务器Job.name的名称,作业的名称,以及作业Job.version的版本。- 此外,所有服务器都以相同的名称开头,例如:dev-1.lan、dev-2.lan、uk-1.lan、us-1.lan 等;


感谢@Korolar,这是一个有效的输入:


   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")

            ))

        );

我想将其转换为表格,以便于阅读。在哪里,我想合并包含相同名称和版本的作业,当版本不同时,再添加一行并指出哪个服务器的版本不同,例如:


森栏
浏览 175回答 1
1回答

暮色呼如

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

相关分类

Java
我要回答