基本的に、各レベルをキーとして、行のリストを値として配置する 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