将Stream转换为IntStream

时间:2018-08-20 06:25:22

标签: java-8 functional-programming java-stream

我正在为两个列表的主地址相等的每个列表项设置id。

服务器POJO

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


    @OneToMany (mappedBy = "server",cascade = CascadeType.ALL, orphanRemoval = true)
    private List<IPAddress> ipaddresses;

    public Integer getId() {
        return id;
    }

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


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

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

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;
    }


}

这是替换重复项的逻辑。

注意::currServerList是数据库和importServerList中的一个列表,我正在将服务器导入到db中,因此当我替换currServerList我要更新服务器(用importServerList替换)时,importServerList将不具有ID重复。

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

            Set<Integer> currServerId = currServerList.stream()
                    .filter(s->primaryIpAddresses.contains(s.getPrimaryIpAddress()))
                    .map(Server::getId)
                    .collect(Collectors.toSet());

            List<Server> filteredList=currServerList.stream()
                    .filter(s->!primaryIpAddresses.contains(s.getPrimaryIpAddress()))
                    .collect(Collectors.toList());
            filteredList.addAll(importServerList);
            filteredList.stream().filter(s->primaryIpAddresses.contains(s.getPrimaryIpAddress())).forEach(s->s.setId(currServerList.stream().filter(server->currServerId.contains(server.getId())).mapToInt(Server::getId)));
  

这是我的逻辑,但是如何将此Stream转换为我尝试过的IntStream   但不起作用    filteredList.stream()。filter(s-> primaryIpAddresses.contains(s.getPrimaryIpAddress()))。forEach(s-> s.setId(currServerList.stream()。filter(server-> currServerId.contains( server.getId()))。mapToInt(Server :: getId)));   编译器错误:服务器类型中的方法setId(Integer)不适用于参数(IntStream)

仍然无法使用,因为我没有将id设置为相等的位置

已更新

filteredList.stream().filter(s->currListPrimaryIpAddress.contains(s.getPrimaryIpAddress()))
            .forEach(srv -> srv.setId(currServerList.stream().filter(server->primaryIpAddresses.contains(server.getPrimaryIpAddress()))
            .findFirst()
            .get().getId())); 

答案中的先前代码为所有服务器设置了相同的ID,但是我尝试不使用像这样的简单for-each循环进行功能编程

for(Server srv : filteredList) {
                for(Server dbsrv : currServerList) {
                    logger.debug("dbsrv ipadd --> " + dbsrv.getPrimaryIpAddress());
                    logger.debug("impsrv ipadd --> " + srv.getPrimaryIpAddress());
                    if(dbsrv.getPrimaryIpAddress()!= null && dbsrv.getPrimaryIpAddress().equals(srv.getPrimaryIpAddress())) {
                        srv.setId(dbsrv.getId());
                        logger.debug("in setting server id");
                        break;
                    }
                }
            }

现在必须检查null,因为它会在那一行引发异常,因为db中有一些数据是错误的,因此我正在做防御性编码,但是现在我需要在上面编写相同的内容使用功能编程编写代码。

1 个答案:

答案 0 :(得分:1)

更改

s->s.setId(currServerList
         .stream()
         .filter(server->currServerId.contains(server.getId()))
         .mapToInt(Server::getId)

s->s.setId(currServerList.stream()
         .filter(server->currServerId.contains(server.getId()))
         .mapToInt(Server::getId)
         .findFirst()
         .orElse(0)

答案是假设.filter(server->currServerId.contains(server.getId()))不匹配任何内容,则默认情况下返回0。

您可以将其更改为适合您的默认值。