You need to sort.
The following code do this, never minds if letters are consecutives or not.
public static void see_string ()
{
List<Pair<Integer,String>> lp=new ArrayList<Pair<Integer,String>>();
Map<String, Integer> mci=new HashMap<String, Integer>();
// Populate
String str="mylongstringtoscanandsomore";
str="BBBBBbbbbbbCCooooPPPu";
int size=str.length();
for (int pos=0;pos<size;pos++)
{
String ch=str.substring(pos,pos+1);
int val=0;
if (mci.containsKey(ch)) {val=mci.get(ch);}
val++;
mci.put(ch, val);
}
// map => list
for (Map.Entry<String, Integer> e : mci.entrySet())
lp.add(new Pair<Integer,String>(e.getValue(),e.getKey()));
// SORT
reverse_sort(lp);
size=lp.size();
// See
for (int pos=0;pos<size;pos++)
{
System.out.println(lp.get(pos).getLeft()+"=>"+lp.get(pos).getRight());
}
}
// http://stackoverflow.com/questions/521171/a-java-collection-of-value-pairs-tuples
public class Pair<L,R> implements java.io.Serializable {
private final L left;
private final R right;
public Pair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() { return left; }
public R getRight() { return right; }
@Override
public int hashCode() { return left.hashCode() ^ right.hashCode(); }
@Override
public boolean equals(Object o) {
if (o == null) return false;
if (!(o instanceof Pair)) return false;
Pair pairo = (Pair) o;
return this.left.equals(pairo.getLeft()) &&
this.right.equals(pairo.getRight());
}
}
// T key to sort
static public <T extends Comparable<T>, Object,K extends Object>
void reverse_sort(List<Pair<T,K>> _list)
{
class ComparaterTK <T extends Comparable<T>,K> implements Comparator<Pair<T,K>>
{
public int compare(Pair<T,K> ob1, Pair<T,K> ob2)
{
return ob2.getLeft().compareTo(ob1.getLeft());
}
}
// simple sort
Collections.sort(_list, new ComparaterTK<T,K>());
}