Java 8:按键合并包含对象的两个列表

时间:2017-01-12 12:02:19

标签: java list java-8 java-stream

我有两个清单:

div:hover {
background-image: url(image-2.jpg);
}

List<Server> servers1 = new ArrayList<>(); Server s1 = new Server("MyServer"); s1.setAttribute1("Attribute1"); servers1.add(s1); List<Server> servers2 = new ArrayList<>(); Server s2 = new Server("MyServer"); s2.setAttribute2("Attribute2"); servers2.add(s2); 包含servers1name(但不是attribute1)的服务器。
attribute2包含servers2name(但没有attribute2)的服务器。

attribute1

有谁知道如何将这两个列表合并到一个包含的列表中 每个public class Server { private String name; private String attribute1; private String attribute2; public Server(String name) { this.name = name; this.attribute1 = ""; this.attribute2 = ""; } //Getters & Setters } 只有一次(按Server),但同时包含两个属性?

有些服务器只存在于一个或另一个列表中。 最终列表应包含所有服务器。

name

应该导致:

[服务器[名称= MyServer,attribute1 = Attribute1,attribute2 = Attribute2],

服务器[name = MyServer2,attribute1 = Attribute1.2,attribute2 =]]

服务器[name = MyServer3,attribute1 =,attribute2 = Attribute2.2]]

//解决方案(大家帮忙!)

    List<Server> servers1 = new ArrayList<>();
    Server s1 = new Server("MyServer");
    s1.setAttribute1("Attribute1");

    Server s2 = new Server("MyServer2");
    s2.setAttribute1("Attribute1.2");

    servers1.add(s1);
    servers1.add(s2);

    List<Server> servers2 = new ArrayList<>();
    Server s3 = new Server("MyServer");
    s3.setAttribute2("Attribute2");

    Server s4 = new Server("MyServer3");
    s4.setAttribute2("Attribute2.2");

    servers2.add(s3);
    servers2.add(s4);

3 个答案:

答案 0 :(得分:2)

将每个列表转换为映射并合并它(我使用Lombok不编写样板代码):

@Data
@NoArgsConstructor
@AllArgsConstructor
class Server {
    private String name;
    private String attribute1;
    private String attribute2;
}

public class ServerMain {

    public static void main(String[] args) {

        List<Server> servers1 = Arrays.asList(
                new Server("name1", "attr1.1", null),
                new Server("name2", "attr1.2", null));

        List<Server> servers2 = Arrays.asList(
                new Server("name1", null, "attr2.1"),
                new Server("name2", null, "attr2.2"));

        Map<String, Server> serverMap1 = servers1.stream().collect(Collectors.toMap(Server::getName, Function.identity()));
        Map<String, Server> serverMap2 = servers2.stream().collect(Collectors.toMap(Server::getName, Function.identity()));

        serverMap1.keySet().forEach(key -> serverMap1.merge(key,
                serverMap2.get(key),
                (server1, server2) -> {
                    server1.setAttribute2(server2.getAttribute2());
                    return server1;
                }));


        System.out.println(serverMap1);
    }
}

答案 1 :(得分:0)

对于servers1中的每个服务器,在servers2中找到匹配的server2,并将第一个的缺失属性设置为更正属性,如果没有匹配的服务器,则只返回null。另请注意,此实现仅返回servers1中包含的服务器名称列表。

func buttonPressed(sender: UIButton){
    print("button pressed")


    if let path = NSBundle.mainBundle().pathForResource("book", ofType: "pdf") {
        if let targetURL = NSURL.fileURLWithPath(path) {

            docController = UIDocumentInteractionController(URL: targetURL)
            let url = NSURL(string:"itms-books:");

            if UIApplication.sharedApplication().canOpenURL(url!) {

                docController!.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)

                print("iBooks is installed")

            }else{

                print("iBooks is not installed")
            }

        }
    }    }

答案 2 :(得分:0)

也许你可以使用地图来做到这一点?地图的关键字是服务器的名称,地图的值是对象Server

Map<String, Server> map = new HashMap<>();
for (Server s : servers1) {
    map.put(s.getName(), s);
}
for (Server s : servers2) {
    String key = s.getName();
    if (map.containsKey(key)) {
        map.get(key).setAttribute2(s.getAttribute2());
    } else {
        map.put(key, s);
    }
}
List<Server> servers = new ArrayList<>(map.values()); // or other impl.