Converting Stream into IntStream

2019-07-25 09:45发布

问题:

I am setting the id for each list item where primary address for two list are equal.

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


}

Here is my logic for replacing duplicates.

Note: currServerList is a list from db and importServerList which I am importing servers into db therefore importServerList will not have id when I replace currServerList I want to update the server(replace with importServerList) rather having duplicates.

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

This is my logic but how do I convert this Stream to IntStream I tried but not working filteredList.stream().filter(s->primaryIpAddresses.contains(s.getPrimaryIpAddress())).forEach(s->s.setId(currServerList.stream().filter(server->currServerId.contains(server.getId())).mapToInt(Server::getId))); Compiler Error: The method setId(Integer) in the type Server is not applicable for the arguments (IntStream)

Still will not work because I am not setting the id where it is equal

UPDATED:

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

Previous code in the answer was setting the same id for all servers but I tried without functional programming using simple for-each loop like this

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

Now checking null was necessary since it was throwing exception at that line since there are some data in db which is wrong therefore I am sort of doing Defensive coding but now I need to write the same above code using functional programming.

回答1:

change

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

to

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

The answer is with the assumption that if .filter(server->currServerId.contains(server.getId())) doesn't match anything, let it return 0 for default condition.

You can change that to what suits your for default value.