Hello,
I have a bit of code that prints the counts of objects in a list. i want to sort it by its count.
code :
output :
[a, a, a, a, a, b, b, c, d]
word : d count : 1
word : b count : 2
word : c count : 1
word : a count : 5
i want to be,
[a, a, a, a, a, b, b, c, d]
word : a count : 5
word : b count : 2
word : d count : 1
word : c count : 1
How to do this?
Thanks and regards
jerald
I have a bit of code that prints the counts of objects in a list. i want to sort it by its count.
code :
Code:
package util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import org.apache.commons.collections.CollectionUtils;
public class Count
{
public static void main(String args[]) throws Exception
{
ArrayList al = new ArrayList();
al.add("a");
al.add("a");
al.add("a");
al.add("a");
al.add("a");
al.add("b");
al.add("b");
al.add("c");
al.add("d");
System.out.println(al);
printcounts(al);
}
private static void printcounts(ArrayList<String> al)
{
HashSet ht = new HashSet(al);
Iterator<String> it = ht.iterator();
while(it.hasNext())
{
String str = it.next();
int count = CollectionUtils.cardinality(str, al);
System.out.println("word : "+str+" count : "+count);
}
}
}
[a, a, a, a, a, b, b, c, d]
word : d count : 1
word : b count : 2
word : c count : 1
word : a count : 5
i want to be,
[a, a, a, a, a, b, b, c, d]
word : a count : 5
word : b count : 2
word : d count : 1
word : c count : 1
How to do this?
Thanks and regards
jerald
Comment