我试图创建一个Hashmap的ArrayList,稍后将使用它。
我正在做的是从前面获得一个名为{strong> groups 的ArrayList<String>
和另一个名为 types 的人。
到目前为止,我已经能够在名为{strong> typeGroup 的ArrayList<HashMap<String, String>>
中获得每种类型及其对应的组,而我得到的结果是typegroup : [{traitement=FC}, {traitement=FP}, {traitement=FE}, {information=ZD}, {information=ZX}, {information=ZY}, {information=ZZ}, {rapport=FP}, {rapport=FD}, {rapport=JD}, {ddm=FD}, {ddm=FT}, {ddm=JD}]
。
我现在想要的是将它们映射到名为 newMap 的ArrayList<HashMap<String, String>>
中,但是这次每种类型都有其对应的组,我得到了这个结果newMap : [{information=ZD,ZX,ZY,ZZ}, {rapport=FP,FD,JD}, {ddm=FD,FT,JD}]
我的问题是它没有得到应该给我这个结果的所有对象newMap : [{traitement=FC,FP,FE}, {information=ZD,ZX,ZY,ZZ}, {rapport=FP,FD,JD}, {ddm=FD,FT,JD}]
这是我的代码:
HashMap<String, Integer> countmap = new HashMap<>();
ArrayList<HashMap<String, String>> typeGroup = new ArrayList<>();
ArrayList<HashMap<String, String>> newMap = new ArrayList<>();
Set<String> ty = new HashSet<>(types);
ArrayList<String> arr = new ArrayList<>(ty);
for (int i = 0; i < arr.size(); i++) {
int count = 0;
for (int j = 0; j < types.size(); j++) {
if (arr.get(i).equals(types.get(j))) {
count++;}
}
countmap.put(arr.get(i), count);
}
System.out.println("arr :"+arr);
System.out.println("countmap : " + countmap);
for (int i = 0; i < types.size(); i++) {
HashMap<String, String> TG = new HashMap<>();
TG.put(types.get(i), groups.get(i));
typeGroup.add(TG);
}
System.out.println("typegroup : " + typeGroup);
boolean test = false;
for (int i = 1; i < typeGroup.size()- 1 ; i++) {
String key = "";
System.out.println((typeGroup.get(i).keySet()).equals(typeGroup.get(i + 1).keySet()));
if ((typeGroup.get(i).keySet()).equals(typeGroup.get(i + 1).keySet())) {
if ((newMap.contains(key))) {
for (String str : typeGroup.get(i).keySet()) {
key = str;
}
HashMap<String, String> op = new HashMap<>();
String val = "";
for (int t1 = 0; t1 < countmap.get(key); t1++) {
if (val == "") {
val = typeGroup.get(i).get(key);
} else {
val = val + "," + typeGroup.get(i+t1).get(key)+"%";
}
op.put(key, val);
}
newMap.add(op);
if (test == false) {
i++;
}
test = false;
System.out.println("++++++++");
}
} else {
test = true;
for (String str : typeGroup.get(i + 1).keySet()) {
key = str;
}
HashMap<String, String> op = new HashMap<>();
String val = "";
for (int t = 0; t < countmap.get(key); t++) {
if (val == "") {
val = typeGroup.get(i + 1).get(key);
} else {
val = val + "," + typeGroup.get(i + 1 + t).get(key);
}
op.put(key, val);
}
System.out.println("op :" +op);
newMap.add(op);
i++;
}
}
System.out.println("countmap : " + countmap);
System.out.println("typeGroup : " + typeGroup);
System.out.println("newMap : " + newMap);
这是我得到的全部结果
types : [traitement, traitement, traitement, information, information, information, information, rapport, rapport, rapport, ddm, ddm, ddm]
groups : [FC, FP, FE, ZD, ZX, ZY, ZZ, FP, FD, JD, FD, FT, JD]
arr :[traitement, rapport, ddm, information]
countmap : {traitement=3, rapport=3, information=4, ddm=3}
typegroup : [{traitement=FC}, {traitement=FP}, {traitement=FE}, {information=ZD}, {information=ZX}, {information=ZY}, {information=ZZ}, {rapport=FP}, {rapport=FD}, {rapport=JD}, {ddm=FD}, {ddm=FT}, {ddm=JD}]
true
false
op :{information=ZD,ZX,ZY,ZZ}
true
true
false
op :{rapport=FP,FD,JD}
true
false
op :{ddm=FD,FT,JD}
true
countmap : {traitement=3, rapport=3, information=4, ddm=3}
typeGroup : [{traitement=FC}, {traitement=FP}, {traitement=FE}, {information=ZD}, {information=ZX}, {information=ZY}, {information=ZZ}, {rapport=FP}, {rapport=FD}, {rapport=JD}, {ddm=FD}, {ddm=FT}, {ddm=JD}]
newMap : [{information=ZD,ZX,ZY,ZZ}, {rapport=FP,FD,JD}, {ddm=FD,FT,JD}]