i have file.txt i would to group data by ip i'm using Collectors.groupingBy
file data
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
i use Collectors.groupingBy to group data so i need to show result like this :
333.000.000=[Newyork,brazil]
200.000.000=[china,icland]
444.000.000=[japon]
the probelme is show result like this :
333.000.000=[class_ip@5b6f7412,class_ip@8b6f7412]
200.000.000=[class_ip@312b1dae,class_ip@6b7f7412]
444.000.000=[class_ip@7530d0]
My code
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);
If you want to display the titles instead of the class_ip
instances, use Collectors.mapping
and generate a Map<String,List<String>>
:
Map<String,List<String>> groupByIP =
array_ip.stream()
.collect(Collectors.groupingBy(class_ip::getIP,
Collectors.mapping(class_ip::getTitle,
Collectors.toList())));