這樣的一個list 我要拼接成的結果是String:
100,300,500
200,300,500
100,400,500
200,400,500
dtoList 是動態的,結果要排序,去重
public class Test {
public static void main(String[] args) {
// 圖片說明
// 這樣的一個list 我要拼接成的結果是String:
// 100,300,500
// 200,300,500
// 100,400,500
// 200,400,500
// dtoList 是動態的,結果要排序,去重
List list = new ArrayList();
list.add(new ValideKeyDTO(100, "省份"));
list.add(new ValideKeyDTO(200, "省份"));
list.add(new ValideKeyDTO(500, "號碼"));
list.add(new ValideKeyDTO(300, "業務"));
list.add(new ValideKeyDTO(400, "業務"));
//
Map> map = new HashMap>();
List list2;
for(ValideKeyDTO v : list){
list2 = map.get(v.getType());
if(null == list2){
list2 = new ArrayList();
map.put(v.getType(), list2);
}
list2.add(v);
}
//
List[] values = map.values().toArray(new List[0]);
StringBuilder str = new StringBuilder();
doPrint(values, 0, str);
}
private static void doPrint(List<ValideKeyDTO>[] values, int i, StringBuilder str) {
for(ValideKeyDTO v : values[i]){
str.append(v.id);
if(values.length>i+1){
str.append(", ");
doPrint(values, i+1, str);
//
str.delete(str.lastIndexOf(v.id+""), str.length());
}else{
System.out.println(str.toString());
int lastIndexOf = str.lastIndexOf(", ");
if(lastIndexOf>0){
str.delete(lastIndexOf+2, str.length());
}else{
str.delete(0, str.length());
}
}
}
}
static class ValideKeyDTO{
long id;
String type;
public ValideKeyDTO(long id, String type) {
this.id = id;
this.type = type;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
}