I am trying to get the count of duplicate values of String ArrayList, i have achieved the task but not completely. i am able to get the counts of duplicate elements of my arrayList, but the problem is that the order of arrayList destroys when i get the occurrences of elements
here is my code:
Map<String, Integer> counts = new HashMap<String, Integer>();
for (String str : t.courseName) {
if (counts.containsKey(str)) {
counts.put(str, counts.get(str) + 1);
} else {
counts.put(str, 1);
}
}
for (Map.Entry<String, Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
this code works fine for getting occurrences but note that this code destroys the order. what i want is that the order should also not be destroyed.
Use
LinkedHashMapinstead ofHashMapto preserve the insertion order