使用Lamda表达式从List <server> userList提取数据

时间:2018-08-17 09:45:40

标签: lambda java-8 functional-programming

我有两个列表importServerList,currServerList

我想使用过滤器方法替换重复项。

服务器POJO

public class Server {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ID")
    public Integer id;

    @Column(name="serverName")
    private String serverName;      

    @OneToMany (mappedBy = "server",cascade = CascadeType.ALL, orphanRemoval = true)
    //@OneToMany(fetch=FetchType.EAGER,cascade = CascadeType.REMOVE, orphanRemoval = true)
    //@JoinColumn(name="server_id", referencedColumnName="ID")
    private List<IPAddress> ipaddresses;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getServerName() {
    return serverName;
    }

    public void setServerName(String serverName) {
    this.serverName = serverName;
    }
    public void setIpaddresses(List<IPAddress> ipaddresses) {
        this.ipaddresses = ipaddresses;
    }

    public List<IPAddress> getIpaddresses() {
        return ipaddresses;
    }

    public String getPrimaryIpAddress() {
        return ipaddresses.stream()
            .filter(IPAddress::isPrimary)
            .findAny()
            .orElseThrow(() -> new IllegalStateException("there should be a primary address"))
            .getIpaddress();
    }

}

这是我的IPAddress POJO

public class IPAddress {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ID")
    public Integer id;

    @Column(name="ipaddress")
    private String ipaddress;

    @Column(name="primaryIP")
    private boolean primary;

    @ManyToOne
    @JsonIgnore
    @JoinColumn(name = "server_id")
    private Server server;


    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }

    public boolean isPrimary() {
        return primary;
    }

    public void setPrimary(boolean primary) {
        this.primary = primary;
    }

    public Server getServer() {
        return server;
    }

    public void setServer(Server server) {
        this.server = server;
    }


}

主要IP地址在每个服务器上都是唯一的

现在,我想要一个没有变异的列表,或者想要使用功能方法更改原始列表中的数据。

我正在检索包含serverList数据的新列表,并从currServerList中删除冗余数据

我尝试过这个东西

Set<String> primaryIpAddresses = currServerList.stream()
                    .map(Server::getPrimaryIpAddress)
                    .collect(Collectors.toSet());
            List<Server> filteredList=importServerList.stream()
                    .filter(s->primaryIpAddresses.contains(s.getPrimaryIpAddress())?s:currlistserverObject)
                    .collect(Collectors.toList()); 

我想它可能会正常工作,但不知道要在s:currlistserverObject中添加什么

目标::从importServerList和currServerList获取具有所有服务器的新列表,但是如果两者都存在冲突或相同的ipaddress,则从importServerList更新服务器,因为服务器名称可能不同,所以我想更新新列表中的importServerList服务器

2 个答案:

答案 0 :(得分:2)

List<Server> newList = new ArrayList<>(importServerList);

Set<String> primary = importServerList.stream()
        .map(Server::getPrimaryIpAddress)
        .collect(Collectors.toSet());

Set<Server> temp = currServerList
        .stream()
        .filter(server -> !primary.contains(server.getPrimaryIpAddress()))
        .collect(Collectors.toSet());

newList.addAll(temp);

答案 1 :(得分:1)

您的过滤器块不正确。如果我正确理解了您的问题(@Eugene所说的不清楚),这就是您所需要的

Set<String> primaryIpAddresses = currServerList.stream()
            .map(Server::getPrimaryIpAddress)
            .collect(Collectors.toSet());
List<Server> filteredList=importServerList.stream()
            .filter(!s->primaryIpAddresses.contains(s.getPrimaryIpAddress()))
            .collect(Collectors.toList());

filteredList.addAll(currServerList);  

在您的问题中,您说的是serverList和currServerList。但是变量被命名为currServerList和importServerList。这很令人困惑。我相信importServerList是指serverList