您可以使用java 8或hibernate。這是對Java 8
public static void main(String[] args) {
List<Item> items = Arrays.asList(
new Item("1", "A", "B", "Cert1"),
new Item("1", "A", "B", "Cert2"),
new Item("2", "C", "D", "Cert3")
);
Map<String, List<Item>> groupByKey = items.stream().collect(Collectors.groupingBy(Item::groupKey));
List<String> result = groupByKey.entrySet().stream()
.map(entry -> entry.getValue().stream().map(Item::d).collect(Collectors.joining(",")))
.collect(Collectors.toList());
result.forEach(System.out::println);
}
public static class Item {
String a;
String b;
String c;
String d;
public Item(String a, String b, String c, String d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
String d() {
return d;
}
String groupKey() {
return a + "-" + b + "-" + c;
}
}
Result :
Cert3
Cert1,Cert2
你爲什麼稱這種操作「過濾器」? – Holger