Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Check count of each character in String  RSS feed

 
sam liya
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get the count of each characters in a String and print the character count  in alphabetical order.
Example
String x= "XABZXXDCCA"

answer should be as below

A 2
C 2
D 1
....

what is the best approach for this ?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach would be to use a loop to get each character and use a Map<String, CounterClass> to save the counts.  CounterClass would be a class you wrote to save the counts in.
Another approach would depend on if the characters are restricted to 26 uppercase letters.  In that case a 26 element array could be used with the char value used to index the array.
 
sam liya
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i print the characters in alphabetical order , i think we need to sort the HashMap
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16043
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of HashMap you could use TreeMap, which automatically sorts its entries by key.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you look at the API doc for the Map interface.  There are related interfaces and/or classes that are sorted.
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Maps, the Java™ Tutorials would also be useful.
 
Rob Spoor
Sheriff
Posts: 21116
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:One approach would be to use a loop to get each character and use a Map<String, CounterClass> to save the counts.

Why not a Map<Character, Integer>? With Java 8 you can even use the compute or merge method:

But you can do it even simpler using streams, although you'll need to use Long for the value type:
 
sam liya
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all , I tried with TreeMap. Now it resolved
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Please show us your solution. Why did you choose a tree map?
 
sam liya
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I choose TreeMap since its already ordered in acending oder.
 
Campbell Ritchie
Marshal
Posts: 56197
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not bad, but you shou‍ld remove the keyword static from just about everywhere. That method is too long, and shou‍ld be divided into several small methods.

I added code tags (please always use them) because they make the code easier to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!