gpt4 book ai didi

java - java中的Collectors.groupingBy结果错误

转载 作者:行者123 更新时间:2023-11-30 06:03:15 24 4
gpt4 key购买 nike

我有 file.txt 我想按 ip 对数据进行分组 我正在使用 Collectors.groupingBy

文件数据

333.000.000,Newyork,50
200.000.000,china,200
333.000.000,brazil,150
444.000.000,japon,40
200.000.000,icland,400

我使用 Collectors.groupingBy 对数据进行分组,因此我需要显示如下结果:

333.000.000=[Newyork,brazil]
200.000.000=[china,icland]
444.000.000=[japon]

探针显示的结果如下:

333.000.000=[class_ip@5b6f7412,class_ip@8b6f7412]
200.000.000=[class_ip@312b1dae,class_ip@6b7f7412]
444.000.000=[class_ip@7530d0]

我的代码

class class_ip{
private String ip;
private String title;
public class_ip(String ip,String title) {
this.ip = ip;
this.title = title;
}
public String getIP() {return ip;}
public String getTitle() {return title;}

public void setTitle(String title) { this.title = title;}
public void setIP(String ip) { this.ip = ip;}

public String getAll() {
return ip+","+title;
}
}

List<class_ip> array_ip = new ArrayList<>();

// read data from file
while ((strLine = br.readLine()) != null) {
array_ip.add(new class_ip(ip,title));
}

Map<String,List<class_ip>> groupByIP = new HashMap<>();
groupByIP =array_ip.stream().collect(Collectors.groupingBy(class_ip::getIP));

System.out.println(groupByIP);

最佳答案

默认返回class_ip@312b1dae Object#toString() ,您需要在class_ip中覆盖它,例如:

@Override
public String toString() {
return getTitle();
}

关于java - java中的Collectors.groupingBy结果错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52625423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com