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" .

1

3 Answers 3

3

It may be helpful

int occ = Collections.frequency(list, "Masum");
Sign up to request clarification or add additional context in comments.

2 Comments

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" .
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.
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());
        }
    })

3 Comments

What will be returned by Collections.Max ? Can you assign that in a variable ?
Can you show the value of String and integer returned by Collections.Max in System.out.println(); ??
@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()
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.