package cn.mw;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class NetworkConnection {
private String srcIp;
private String dstIp;
private int srcPort;
private int dstPort;
public NetworkConnection() {
}
public NetworkConnection(String srcIp, String dstIp, int srcPort, int dstPort) {
this.srcIp = srcIp;
this.dstIp = dstIp;
this.srcPort = srcPort;
this.dstPort = dstPort;
}
public String getSrcIp() {
return srcIp;
}
public void setSrcIp(String srcIp) {
this.srcIp = srcIp;
}
public String getDstIp() {
return dstIp;
}
public void setDstIp(String dstIp) {
this.dstIp = dstIp;
}
public int getSrcPort() {
return srcPort;
}
public void setSrcPort(int srcPort) {
this.srcPort = srcPort;
}
public int getDstPort() {
return dstPort;
}
public void setDstPort(int dstPort) {
this.dstPort = dstPort;
}
// 省略构造函数和其他方法
// Getter 和 Setter 方法
}
public class ConnectionGrouper {
public static void main(String[] args) {
// 假设你有一个 List<NetworkConnection> connections
List<NetworkConnection> connections = Arrays.asList(
new NetworkConnection("192.168.1.1", "192.168.2.1", 8080, 80),
new NetworkConnection("192.168.1.2", "192.168.2.2", 8081, 81),
new NetworkConnection("192.168.1.3", "192.168.2.3", 8081, 81),
new NetworkConnection("192.168.1.4", "192.168.2.4", 8081, 81),
new NetworkConnection("192.168.1.5", "192.168.2.5", 8081, 81),
new NetworkConnection("192.168.1.6", "192.168.2.6", 8081, 81)
);
// 使用Java Stream API进行分组
Map<String, List<String>> groupedConnections = connections.stream()
.flatMap(connection -> Stream.of(
new AbstractMap.SimpleEntry<>("src_ip", String.valueOf(connection.getSrcIp())),
new AbstractMap.SimpleEntry<>("dst_ip", String.valueOf(connection.getDstIp())),
new AbstractMap.SimpleEntry<>("src_port", String.valueOf(connection.getSrcPort())),
new AbstractMap.SimpleEntry<>("dst_port", String.valueOf(connection.getDstPort()))
))
.filter(entry -> !entry.getValue().equals("null"))
.collect(Collectors.groupingBy(
Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())
));
// 输出结果
groupedConnections.forEach((key, value) -> System.out.println(key + ": " + value));
}
}
标签:中多字,java,String,192.168,分组,NetworkConnection,new,dstPort,public From: https://www.cnblogs.com/xxsdnol/p/17874996.html