将Stream转换为IntStream

将Stream转换为IntStream

本文介绍了将Stream转换为IntStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为每个列表项设置id,其中两个列表的主地址相等。

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

服务器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

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.

注意: currServerList是db和importServerList的列表,我将服务器导入db,因此当我替换currServerList时,importServerList将没有id想要更新服务器(替换为importServerList)而不是重复。

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



仍然无效,因为我没有设置id相等的地方

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

更新:

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

答案中的前一代码是为所有服务器设置相同的ID,但我尝试使用简单的函数编程像这样的每个循环

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

现在检查null是必要的因为db中有一些数据是错误的,所以它在那条线上抛出异常因此我正在做防御性编码,但现在我需要使用函数式编程编写相同的代码。

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.

推荐答案

更改

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.conta ins(server.getId()))与任何内容都不匹配,让它在默认条件下返回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.

这篇关于将Stream转换为IntStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:46