This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Check count of each character in String  RSS feed

 
Ranch Hand
Posts: 1313
1
  • 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 ?
 
Rancher
Posts: 3354
33
  • 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: 1313
1
  • 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
 
Java Cowboy
Posts: 16084
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: 3354
33
  • 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.
 
Marshal
Posts: 64494
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Maps, the Java™ Tutorials would also be useful.
 
Sheriff
Posts: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 1313
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all , I tried with TreeMap. Now it resolved
 
Campbell Ritchie
Marshal
Posts: 64494
225
  • 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: 1313
1
  • 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: 64494
225
  • 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.
 
What's that smell? Hey, sniff this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!