This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes List contents - number of occurrences ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "List contents - number of occurrences ?" Watch "List contents - number of occurrences ?" New topic
Author

List contents - number of occurrences ?

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1396
List al = new ArrayList();
al.add("James");
al.add("Bill");
al.add("Joe");
al.add("Bill");
al.add("James");
al.add("Joe");
al.add("James");
al.add("Joe");
al.add("steve");
al.add("Joe");

Which is the best optimized way to get each elements number of occurrences like James occurs 3 times, Joe occurs 4 times, Steve occur 1 time.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20032
    
  44


This is a pretty common homework problem... so why don't you tell us what you have done so far? And if still in the design phase, what algorithm you are currently considering?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1396
I am planning to use HashSet for storing unique values. Then i will create duplicate keys in the Map based on one of the previous thread.



Finally planning to check the map values with HashSet unique values for incrementing the counter value.
antty ge
Greenhorn

Joined: Dec 24, 2010
Posts: 7
I finish it :


syso Result:
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1396


'result' HashMap does not contain any elements. How containsKey() works here ?
 
Have you checked out Aspose?
 
subject: List contents - number of occurrences ?
 
jQuery in Action, 3rd edition