日期:2014-05-20 浏览次数:20724 次
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class t1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map<String,String> m1 = new HashMap<String, String>();
m1.put("id", "A1");
m1.put("p_id", "0");
Map<String,String> m2 = new HashMap<String, String>();
m2.put("id", "A2");
m2.put("p_id", "0");
Map<String,String> m3 = new HashMap<String, String>();
m3.put("id", "A3");
m3.put("p_id", "A1");
Map<String,String> m4 = new HashMap<String, String>();
m4.put("id", "A4");
m4.put("p_id", "A1");
Map<String,String> m5 = new HashMap<String, String>();
m5.put("id", "A5");
m5.put("p_id", "A2");
Map<String,String> m6 = new HashMap<String, String>();
m6.put("id", "A6");
m6.put("p_id", "A3");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
list.add(m5);
list.add(m6);
List<Map<String,String>> result = sort(list,"0");
for (Map<String, String> map : result) {
System.out.println(map.get("id")+","+map.get("p_id"));
}
}
public static List<Map<String,String>> sort(List<Map<String,String>> list,String id){
List<Map<String,String>> result = new ArrayList<Map<String,String>>();
for (Map<String, String> map : list) {
if(id.equals(map.get("p_id"))){
result.add(map);
result.addAll(sort(list,map.get("id")));
}
}
return result;
}
}