Java 使用哪种收集器?
关于这些类,我正在尝试构建一个地图: 使用这些简单的类结构Java 使用哪种收集器?,java,lambda,java-8,java-stream,collectors,Java,Lambda,Java 8,Java Stream,Collectors,关于这些类,我正在尝试构建一个地图: 使用这些简单的类结构 class Alert { public String serie; public String rame; public String label; public Alert( String serie, String rame, String label ) { this.serie = serie; this.rame = rame;
class Alert {
public String serie;
public String rame;
public String label;
public Alert( String serie, String rame, String label ) {
this.serie = serie;
this.rame = rame;
this.label = label;
}
public String getSerie() {
return serie;
}
public String getRame() {
return rame;
}
public String getLabel() {
return label;
}
}
class Diagnostic {
public String serie;
public String rame;
public List< Event > events;
public Diagnostic(String serie, String rame){
this.serie = serie;
this.rame = rame;
}
public void addEvent(String label){
if(events == null)events = new ArrayList<>( );
events.add( new Event( label ) );
}
}
class Event {
public String label;
public Event(String label){
this.label = label;
)}
}
Alert atgv1 = new Alert( "tgv", "rame1", "label1" );
Alert atgv2 = new Alert( "tgv", "rame1", "label2" );
Alert atgv3 = new Alert( "tgv", "rame1" , "label3");
Alert atgv4 = new Alert( "tgv", "rame2", "label4" );
Alert atgv5 = new Alert( "tgv", "rame2", "label5" );
Alert atgv6 = new Alert( "tgv", "rame3", "label6" );
Alert az2n1 = new Alert( "z2n", "rame1", "label1" );
Alert az2n2 = new Alert( "z2n", "rame1", "label2" );
Alert az2n3 = new Alert( "z2n", "rame1" , "label3");
Alert az2n4 = new Alert( "z2n", "rame2", "label4" );
Alert az2n5 = new Alert( "z2n", "rame2", "label5" );
Alert az2n6 = new Alert( "z2n", "rame3", "label6" );
List<Alert> alerts = Arrays.asList(atgv1, atgv2, atgv3, atgv4, atgv5, atgv6, az2n1, az2n2, az2n3, az2n4, az2n5, az2n6);
类警报{
公共字符串系列;
公共字符串;
公共字符串标签;
公共警报(字符串序列、字符串帧、字符串标签){
this.serie=serie;
this.rame=rame;
this.label=标签;
}
公共字符串getSerie(){
返回意甲;
}
公共字符串getRame(){
返回帧;
}
公共字符串getLabel(){
退货标签;
}
}
类诊断{
公共字符串系列;
公共字符串;
公共列表<事件>事件;
公共诊断(字符串序列、字符串帧){
this.serie=serie;
this.rame=rame;
}
公共void addEvent(字符串标签){
如果(events==null)events=newarraylist();
添加(新事件(标签));
}
}
班级活动{
公共字符串标签;
公共事件(字符串标签){
this.label=标签;
)}
}
警报atgv1=新警报(“tgv”、“rame1”、“label1”);
警报atgv2=新警报(“tgv”、“rame1”、“label2”);
警报atgv3=新警报(“tgv”、“rame1”、“label3”);
警报atgv4=新警报(“tgv”、“rame2”、“label4”);
警报atgv5=新警报(“tgv”、“rame2”、“label5”);
警报atgv6=新警报(“tgv”、“rame3”、“label6”);
警报az2n1=新警报(“z2n”、“rame1”、“label1”);
警报az2n2=新警报(“z2n”、“rame1”、“label2”);
警报az2n3=新警报(“z2n”、“rame1”、“label3”);
警报az2n4=新警报(“z2n”、“rame2”、“label4”);
警报az2n5=新警报(“z2n”、“rame2”、“label5”);
警报az2n6=新警报(“z2n”、“rame3”、“label6”);
列表警报=Arrays.asList(atgv1、atgv2、atgv3、atgv4、atgv5、atgv6、az2n1、az2n2、az2n3、az2n4、az2n5、az2n6);
我有这样的代码:
Map<String, Map<String, Diagnostic>> map = alerts.stream()
.collect(Collectors.collectingAndThen( Collectors.groupingBy( Alert::getSerie,
Collectors.toMap( Alert::getRame, a -> new Diagnostic( a.getSerie(), a.getRame() ), (a1, a2) -> a1)), b -> {
b.forEach( ( k, v ) -> v.forEach( ( y, z) -> {
List<Alert> found = alerts.stream().filter( p -> p.getSerie().equals( k ) && p.getRame().equals( y ) ).collect( Collectors.toList());
found.forEach( f -> z.addEvent( f.getLabel() ) );
}) );
return b;
} ));
Map Map=alerts.stream()
.collect(收集器.collectingAndThen(收集器.groupingBy)(警报::getSerie,
Collectors.toMap(Alert::getRame,a->new Diagnostic(a.getSerie(),a.getRame(),(a1,a2)->a1)),b->{
b、 forEach((k,v)->v.forEach((y,z)->{
找到的列表=alerts.stream().filter(p->p.getSerie().equals(k)和&p.getRame().equals(y)).collect(Collectors.toList());
found.forEach(f->z.addEvent(f.getLabel());
}) );
返回b;
} ));
结果就是我想要的,但问题是我在中的映射上循环,然后
以找到正确的警报(过滤器)并添加事件(.addEvent)
如何一次完成?是否可以不编写我自己的收集器
?
困难的部分是我想要一个带有事件列表的单一诊断,而不是一个可以通过第二个
groupingBy
而不是toMap
轻松实现的诊断列表。您可以通过多个groupingBy
来解决问题,然后,您要查找的是减少诊断值:
Map<String, Map<String, Optional<Diagnostic>>> map = alerts.stream()
.collect(Collectors.groupingBy(Alert::getSerie,
Collectors.groupingBy(Alert::getRame,
Collectors.mapping(e -> new Diagnostic(e.getSerie(), e.getRame()),
Collectors.reducing((d1, d2) -> Diagnostic.builder()
.serie(d1.getSerie())
.rame(d1.getRame())
.events(Stream.concat(d1.getEvents().stream(),
d2.getEvents().stream()).collect(Collectors.toList()))
.build())))));
Map<String, Map<String, Diagnostic>> map = alerts.stream()
.collect(Collectors.groupingBy(Alert::getSerie,
Collectors.toMap(Alert::getRame, e -> new Diagnostic(e.getSerie(), e.getRame()),
(d1, d2) -> Diagnostic.builder()
.serie(d1.getSerie())
.rame(d1.getRame())
.events(Stream.concat(d1.getEvents().stream(),
d2.getEvents().stream()).collect(Collectors.toList()))
.build())));