i have got a string of names
String str = "A. Walker, L. Gordon, C. Riley, L. Gordon";
I need to count name occurancies and sort the occurancies from biggest to lowest.
I have done the countung part, but I also need to sort it.
String[] array = str.split(", ");
List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s: mySet)
System.out.println(s + " " +Collections.frequency(asList,s));
Output should look like this
L. Gordon 2, A. Walker 1, C. Riley 1
LinkedHashSetinstead ofHashSetto preserve insertion order, and sortasListbefore you pass it to the set. (Note, useList<String> asListrather thanList asList).java count duplicates.TreeMultisetis the perfect ADT for this.