求解题思路
A集合中有不定量的单条数据 , 需要存到B集合中 ,
B集合中的每一条都包含A集合的多条数据
基于Monster 组和GPT的调写:
map
List<A> aList = ...; // A集合
List<Map<String, List<A>>> bList = new ArrayList<>(); // B集合
for (A a : aList) {
// 从A对象中获取某个属性作为键,例如ID
String key = a.getId();
// 在B集合中查找包含该键的Map
Map<String, List<A>> map = null;
for (Map<String, List<A>> m : bList) {
if (m.containsKey(key)) {
map = m;
break;
}
}
// 如果找不到对应的Map,则创建一个新的Map并添加到B集合中
if (map == null) {
map = new HashMap<>();
map.put(key, new ArrayList<>());
bList.add(map);
}
// 将A对象添加到Map中
map.get(key).add(a);
}