0

Let us assume I have the following list .

List<String> list=new ArrayList<String>();
list.add("sultan");
list.add("Masum");
list.add("sultan");
list.add("Sorry");
list.add("sultan");
list.add("Masum");
list.add("sultan");
list.add("Tarek");
list.add("sultan");

I want to know the count of occurrence of each string in Arraylist . How can I do that ? And I also want to know the string that is occurred in this Arraylist at highest times. For this particular example, the answer is "Sultan" .

jww
  • 97,681
  • 90
  • 411
  • 885
osimer pothe
  • 2,827
  • 14
  • 54
  • 92
  • 1
    possible duplicate of [How to count occurrence of an element in a List](http://stackoverflow.com/questions/505928/how-to-count-occurrence-of-an-element-in-a-list) – Abimaran Kugathasan Feb 07 '14 at 07:05

3 Answers3

3

It may be helpful

int occ = Collections.frequency(list, "Masum");
wawek
  • 1,577
  • 1
  • 13
  • 23
  • And I also want to know the string that is occurred in this arraylist at highest times . For this particular example , the answer is "Sultan" . – osimer pothe Feb 07 '14 at 07:05
  • Iterate over list comparing each `occ` with `maximum` having remembered in memory. Of course if `occ > maximum` then you should write somewhere the actual list's element value. – wawek Feb 07 '14 at 07:10
2

If you want all the strings in the first list something you could do is:

import java.util.*;

import java.util.Map.Entry;
//...

Set<String> uniques = new HashSet(list);
Map<String, Integer> counts = new HashMap<String, Integer>();

for (String elem : uniques) {
    counts.put(elem, Collections.frequency(list, elem));
}

So in the end you will have the count for each string in the map. Putting one to one...this would do the following:

  • by creating the set from the initial list you will have each string from your list, afterwards you "walk" your list and compute for each the frequency for any given string.

As for the highest frequency, you could use the Collections.max method on the entry set like this:

Collections.max(counts.entrySet(), new Comparator<Entry<String, Integer>>() {
        @Override
        public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
            return (o1.getValue() - o2.getValue());
        }
    })
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
  • What will be returned by Collections.Max ? Can you assign that in a variable ? – osimer pothe Feb 07 '14 at 10:53
  • Can you show the value of String and integer returned by Collections.Max in System.out.println(); ?? – osimer pothe Feb 07 '14 at 11:23
  • @osimerpothe - the output will be key = value. On your output: sultan=5. If you need the string: the result of max .getKey() and its count: the result of the max .getValue() – Olimpiu POP Feb 07 '14 at 12:40
1

Check this

Collections.frequency and THIS EXAMPLE

from this example:

System.out.println("\nExample 1 - Count 'a' with frequency");
System.out.println("a : " + Collections.frequency(list, "a"));

System.out.println("\nExample 2 - Count all with frequency");
Set<String> uniqueSet = new HashSet<String>(list);
for (String temp : uniqueSet) {
    System.out.println(temp + ": " + Collections.frequency(list, temp));
}

System.out.println("\nExample 3 - Count all with Map");
Map<String, Integer> map = new HashMap<String, Integer>();

for (String temp : list) {
    Integer count = map.get(temp);
    map.put(temp, (count == null) ? 1 : count + 1);
}
printMap(map);

System.out.println("\nSorted Map");
Map<String, Integer> treeMap = new TreeMap<String, Integer>(map);
printMap(treeMap);
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147