This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 1372
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: 18754
    
  40


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: 1372
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: 1372


'result' HashMap does not contain any elements. How containsKey() works here ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List contents - number of occurrences ?