Chcesz to w javie połączyć za pomocą kodu?
import java.util.*;
import java.util.stream.*;
public class HelloWorld {
public static void main(String []args) {
List<MyDTO> list = Arrays.asList(
new MyDTO(2134, false, "test1"),
new MyDTO(2134, false, "test2"),
new MyDTO(1111, false, "test34")
);
Map<KeyMyDTO, List<MyDTO>> map = list.stream()
.collect(Collectors.groupingBy(i -> i.toKey()));
List<MyDTO> merged = map.entrySet().stream()
.map(e -> new MyDTO(e.getKey(), MyDTO.extractTers(e.getValue())))
.collect(Collectors.toList());
System.out.println(merged);
}
}
class MyDTO {
int id;
boolean active;
String[] ter;
MyDTO(int id, boolean active, String ... ter) {
this.id = id;
this.active = active;
this.ter = ter;
}
MyDTO(KeyMyDTO key, String[] ter) {
this.id = key.id;
this.active = key.active;
this.ter = ter;
}
KeyMyDTO toKey() {
return new KeyMyDTO(id, active);
}
static String[] extractTers(List<MyDTO> list) {
return list.stream()
.flatMap(a -> Arrays.asList(a.ter).stream()).toArray(String[]::new);
}
public String toString() {
return "{id: " + id + ", active: " + active + ", ter: [" + String.join(", ", ter) + "]}" ;
}
}
class KeyMyDTO {
int id;
boolean active;
KeyMyDTO(int id, boolean active) {
this.id = id;
this.active = active;
}
public boolean equals(Object that) {
return that != null && that.getClass() == getClass() && eq((KeyMyDTO)that);
}
boolean eq(KeyMyDTO that) {
return that.id == id && that.active == active;
}
public int hashCode() {
return id * 2 + (active ? 1 : 0);
}
}
Już zapomniałem jaka Java jest rozwlekła :D