-1

List is

List<String> list = new ArrayList<String>();

list.add("apple");
list.add("ball");
list.add("apple");
list.add("cat");
list.add("ball");

Now I have to sort this list with frequency of the apple, ball, cat

I have to get output as:

apple
ball
cat
Pshemo
  • 122,468
  • 25
  • 185
  • 269
MVA
  • 11
  • 1
  • 1
  • not understand you question.... – Janny Mar 13 '13 at 05:47
  • 1
    Please share what you've tried and we'll be happy to help. You might want to try google as well. If I search for "_java sort list by frequency_" the first result brings me right to a great answer on this site. – jahroy Mar 13 '13 at 05:49
  • 1
    Already answered in the same forum. Refer here http://stackoverflow.com/questions/10158793/sorting-words-in-order-of-frequency-least-to-greatest – Joe2013 Mar 13 '13 at 05:52
  • 2
    *Hint:* use Google Guava's `MultiSet` which already counts the frequencies for you. – Narendra Pathai Mar 13 '13 at 05:56
  • 1
    use collections.frequency() to count occuranses http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Collections.html#frequency(java.util.Collection, java.lang.Object) . the create a hash map han sort it according to value – Bhavik Shah Mar 13 '13 at 06:09

4 Answers4

2

First, count the occurrence of string and then sort by using Map

List<String> list = new ArrayList<String>();
list.add("apple");
list.add("ball");
list.add("apple");
list.add("cat");
list.add("ball");
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : list) {
    if (map.containsKey(s)) {
        map.put(s, map.get(s) + 1);
    } else {
        map.put(s, 1);
    }
}
ValueComparator<String, Integer> comparator = new ValueComparator<String, Integer> (map);
Map<String, Integer> sortedMap = new TreeMap<String, Integer> (comparator);
sortedMap.putAll(map);

List<String> sortedList = new ArrayList<String> (sortedMap.keySet());

System.out.println(sortedMap);
System.out.println(sortedList);

}

static class ValueComparator<K, V extends Comparable<V>> implements Comparator<K> {

Map<K, V> map;

public ValueComparator(Map<K, V> base) {
    this.map = base;
}

@Override
public int compare(K o1, K o2) {
     return map.get(o2).compareTo(map.get(o1));
}
}
Linga
  • 10,379
  • 10
  • 52
  • 104
0

You can create a customized list containing a map that registers the frequency of an element in the list.

pulic class FrequencySortList<E> extend ArrayList<E> {

  private Map<E,Integer> frequency = new HashMap<E,Integer>();


  .....




}
Foredoomed
  • 2,219
  • 2
  • 21
  • 39
0

Try this,

List list = new ArrayList();
         list.add("apple");
         list.add("ball");
         list.add("apple");
         list.add("cat");
         list.add("ball");

         // sort the list
         Collections.sort(list);

Here your list contains:

apple
apple
ball
ball
cat
         List list2 = new  ArrayList();

         for(int i=0;i<list.size();i++)
         {
             if(!list2.contains(list.get(i)))
             {
                 list2.add(list.get(i));
             }

         }
         for(int i=0;i<list2.size();i++)
         {

             System.out.println(list2.get(i));
         }

list2 will print:

apple
ball
cat

or you can do the same thing with using this single line as jahroy mentioned in comment

List list2 = new ArrayList<String>(new TreeSet<String>(list));
subodh
  • 6,136
  • 12
  • 51
  • 73
  • The OP would like to sort the list by the number of occurrences of each element. This will generate a list of unique elements sorted alphabetically. Also, you could achieve the same effect of the above code like this: `Set s = new TreeSet(list);`, which will create a sorted collection of unique elements. – jahroy Mar 13 '13 at 06:06
0

first you need to make simple class as your model :

public class Thing {

        String name;
        int total;

        public Thing(String name){
            this.name=name;
            total=1;
        }

       //setter and getter
    }

then you need to calculate how many frequency of that thing appears :

List list = new ArrayList();
list.add("apple");
list.add("ball");
list.add("apple");
list.add("cat");
list.add("ball");
System.out.println(list.size());
List list2 = new ArrayList();
for(int i=0;i<list.size();i++){
    System.out.println(list.get(i));
    Thing thing= new Thing(list.get(i).toString());
    if(list2.size()<1){
        list2.add(thing);
    }else {
        boolean insert=true;
        int x=0;
        for(int j=0;j<list2.size();j++){
            Thing toCompare=(Thing)list2.get(j);
            if(toCompare.getName().equals(thing.getName())){
                insert=false;
                x=j;
                thing.setTotal(thing.getTotal()+1);
                break;
            }
        }
        if(insert==true){
            list2.add(thing);
        }else{
            list2.set(x, thing);
        }
    }
}

for(int i=0;i<list2.size();i++){
    Thing thing=(Thing)list2.get(i);
    System.out.println(thing.getName()+" : "+thing.getTotal());
}
Daniel Robertus
  • 1,100
  • 1
  • 11
  • 24