基本上,您必须使用 Map 将每个级别作为键并将行列表作为值。处理完要合并的列表后,获取键集并按降序对其进行排序,以便对级别进行排序。然后,您可以使用排序的键/级别开始迭代映射值。将级别添加到 dest 列表中,对实际级别的列表进行排序并将其所有元素也添加到 dest。
这是我想出的:
public class MergeLists {
private final static String[] list1 = {
"==level 1==\r\n",
"1.1 2\r\n",
"\r\n",
"\r\n",
"==level 2==\r\n",
"1.R4 0\r\n",
"2.0 2\r\n",
"\r\n",
"\r\n",
"==level 3==\r\n",
"3.R3 1\r\n",
"4.R5 1"
};
private final static String[] list2 = {
"==level 3==\r\n",
"1.null\r\n",
"2.null"
};
@Test
public void mergLists() {
List<List<String>> listList = new ArrayList<>();
listList.add(Arrays.asList(list1));
listList.add(Arrays.asList(list2));
List<String> mergedList = mergLists(listList);
for(String s : mergedList) {
System.out.println(s);
}
}
public List<String> mergLists(List<List<String>> listList) {
List<String> mergedList = new ArrayList<>();
Map<String, List<String>> levelMap = new HashMap<String, List<String>>();
for(int j = 0; j < listList.size(); j++) {
List<String> list = listList.get(j);
String actLevel = null;
for(int i = 0; i < list.size(); i++) {
String line = list.get(i).trim();
if(isLevel(line)) {
actLevel = line;
} else {
if(actLevel != null) {
List<String> levelList = levelMap.get(actLevel);
if(levelList == null) {
levelList = new ArrayList<>();
levelMap.put(actLevel, levelList);
}
levelList.add(line);
} else {
System.out.println("line " + (i+1) + " in list " + (j+1) + " does not belong to a level.");
}
}
}
}
List<String> sortedLevelList = new ArrayList<>(levelMap.keySet());
Collections.sort(sortedLevelList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return (extractNumberFromLevel(o1) - extractNumberFromLevel(o2));
}
private int extractNumberFromLevel(String level) {
// check that this meets the format of your level entry (assuming "==level n==")
int r = 0;
int i = level.indexOf("level");
if(i != -1) {
int j = level.lastIndexOf("==");
if(j != -1) {
String n = level.substring(i + "level".length(), j).trim();
try {
r = Integer.parseInt(n);
} catch(NumberFormatException e) {
// ignore and return 0
}
}
}
return r;
}
});
for(String level : sortedLevelList) {
List<String> lineList = levelMap.get(level);
Collections.sort(lineList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.trim().length() == 0 ? 1 /* this puts the empty string at the end of the list */
: (extractNumberFromLine(o1) - extractNumberFromLine(o2));
}
private int extractNumberFromLine(String o) {
// check that this meets the format of your line entry (assuming "n.1 2")
int r = 0;
int i = o.indexOf('.');
if(i != -1) {
String n = o.substring(0, i).trim();
try {
r = Integer.parseInt(n);
} catch(NumberFormatException e) {
// ignore and return 0
}
}
return r;
}
});
mergedList.add(level);
mergedList.addAll(lineList);
}
return mergedList;
}
private boolean isLevel(String line) {
// check that this meets the format of your level entry (assuming "==level n==")
return line.contains("level");
}
}
输出
==level 1==
1.1 2
==level 2==
1.R4 0
2.0 2
==level 3==
1.null
2.null
3.R3 1
4.R5 1