public class VerticalMerger {
public static List> transform(
LinkedList keyList, List> mapList) {
List> newMapList = new LinkedList>();
Map newMap = null;
List> keyMapList = null;
Map keyMap = null;
// 判断是否有需要合并的列
if (!keyList.isEmpty()) {
String key = keyList.getFirst();
Object value = null;
for (Map map : mapList) {
Object _value = map.get(key);
// 遇到合并列出现不同的值时,建立新的下级列表
if (null == _value || "".equals(_value)
|| !_value.equals(value)) {
keyMapList = new LinkedList>();
newMap = new HashMap();
newMap.put(key, value = _value);
newMap.put("children", keyMapList);
newMapList.add(newMap);
}
keyMap = new HashMap();
for (Map.Entry entry : map.entrySet()) {
if (!key.equals(entry.getKey())) {
keyMap.put(entry.getKey(), entry.getValue());
}
}
keyMapList.add(keyMap);
}
for (Map map : newMapList) {
keyMapList = (List>) map.get("children");
if (keyList.size() > 1) {
keyMapList = transform(new LinkedList(keyList
.subList(1, keyList.size())), keyMapList);
}
map.put("children", keyMapList);
Integer length = 0;
for (Map _keyMap : keyMapList) {
if (_keyMap.containsKey("length")) {
length += (Integer) _keyMap.get("length");
} else {
length = keyMapList.size();
}
}
map.put("length", length);
}
return newMapList;
}
return mapList;
}
}