File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 1365
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: 18101
    
  39


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


'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 ?
 
Similar Threads
Adding to generic collection
Skipping element addition in list
search string from bigger string
ArrayList sorting using Ccomparator
Help me solve a puzzle on datastructures