java 对List<ScheckTemplatesItemSpecific> itemList和子集List<ScheckTemplatesContentSpecific> contentList多条数据,按1 、1.1 1.2 1.3 、2 2.1 2.2 重新排序 更新到数据库
时间: 2024-03-07 19:46:25 浏览: 11
可以使用如下代码将重新排序后的结果更新到数据库中:
```java
// 假设数据库中已经存在 itemList 和 contentList 的数据
// 使用上面的代码对 itemList 和 contentList 进行重新排序
// 更新 itemList
for (ScheckTemplatesItemSpecific item : itemList) {
// 根据 itemNumber 更新数据库中对应的记录
updateItem(item);
}
// 更新 contentList
for (ScheckTemplatesContentSpecific content : contentList) {
// 根据 contentNumber 更新数据库中对应的记录
updateContent(content);
}
```
这段代码假设已经有了更新 itemList 和 contentList 的方法 `updateItem` 和 `updateContent`,这里就不做详细介绍了。需要注意的是,更新数据库时要按照编号顺序依次更新,以保证更新后的数据顺序正确。
相关问题
java 对List<ScheckTemplatesItemSpecific> itemList和子集List<ScheckTemplatesContentSpecific> contentList多条数据,按1 、1.1 1.2 1.3 、2 2.1 2.2 重新排序
可以使用如下代码对 itemList 和 contentList 进行重新排序:
```java
// 对 itemList 进行排序
Collections.sort(itemList, new Comparator<ScheckTemplatesItemSpecific>() {
@Override
public int compare(ScheckTemplatesItemSpecific o1, ScheckTemplatesItemSpecific o2) {
return o1.getItemNumber().compareTo(o2.getItemNumber());
}
});
// 对 contentList 进行排序
Collections.sort(contentList, new Comparator<ScheckTemplatesContentSpecific>() {
@Override
public int compare(ScheckTemplatesContentSpecific o1, ScheckTemplatesContentSpecific o2) {
return o1.getContentNumber().compareTo(o2.getContentNumber());
}
});
// 对 contentList 进行分组
Map<String, List<ScheckTemplatesContentSpecific>> contentMap = new HashMap<>();
for (ScheckTemplatesContentSpecific content : contentList) {
String[] arr = content.getContentNumber().split("\\.");
String key = arr[0];
List<ScheckTemplatesContentSpecific> list = contentMap.get(key);
if (list == null) {
list = new ArrayList<>();
contentMap.put(key, list);
}
list.add(content);
}
// 合并 itemList 和 contentList
List<Object> resultList = new ArrayList<>();
int i = 0;
for (ScheckTemplatesItemSpecific item : itemList) {
resultList.add(item);
String key = item.getItemNumber();
List<ScheckTemplatesContentSpecific> list = contentMap.get(key);
if (list != null) {
Collections.sort(list, new Comparator<ScheckTemplatesContentSpecific>() {
@Override
public int compare(ScheckTemplatesContentSpecific o1, ScheckTemplatesContentSpecific o2) {
return o1.getContentNumber().compareTo(o2.getContentNumber());
}
});
resultList.addAll(list);
}
}
```
这段代码首先对 itemList 和 contentList 分别按照编号进行排序,然后对 contentList 进行分组,将同一个父级编号的子集放在一起。最后,将 itemList 和对应的子集合并到一起,得到重新排序后的结果。
我有一个List<PriceProtocolItem> itemList 结构 和Map<id,PriceProtocol> map 结构,itemList中PriceProtocolItem需要setProtocol,怎么处理呢
可以遍历itemList中的每个PriceProtocolItem,然后根据其id在map中查找对应的PriceProtocol,最后将该PriceProtocol设置为PriceProtocolItem的protocol属性。具体代码实现如下:
```
for (PriceProtocolItem item : itemList) {
PriceProtocol protocol = map.get(item.getId());
item.setProtocol(protocol);
}
```