• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to count the frequency of each string within the arraylist

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to count the frequency of each string stored in the array List. My arraylist contains the following strings:

PUSH ADD ADD ADD AND AND AND AND AND CALL CALLDATACOPY CALLDATALOAD
CALLDATALOAD CALLDATALOAD CALLDATASIZE CALLDATASIZE CALLER
CALLVALUE  DIV  DIV DIV


I found the frequency function at:
frequency function:

public static int frequency(Collection<?> c, Object o)

but I can’t understand how to use it. I have written the following code:



Somebody please guide me.

Zulfi.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I can’t understand how to use it


Have you read the API for that method?  Does it look like it would be of any use?
If you have any questions about the API doc for the method, copy the text of the API doc and paste it here with your questions.

One issue I see with the code is that the value returned by the frequency method is not saved.
Another issue is the number of passes over the list your technique requires.

Another approach is to use a Map<String, Counter>  where the key is the String from the list and Counter holds the count of the number of times it was found in the list.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Another approach is to use a Map<String, Counter>  where the key is the String from the list and Counter holds the count of the number of times it was found in the list.


If you use Map<String, IntAdder> you don't need to create a Counter class yourself. Of course, you can also use streams to get a Map<String, Long> quite easily (hint: it uses Collectors.counting).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic