File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Array List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array List" Watch "Array List" New topic

Array List

James Tok

Joined: May 19, 2010
Posts: 4
Good day,
I have a list as below


how can i get something as below: which the number indicates the # repeated in the list?

i only know that i could use HashSet to eliminate the duplicates... but how to display the number?


Carey Brown
Ranch Hand

Joined: Nov 19, 2001
Posts: 885

Instead of HashSet, use HashMap with a value of type Integer to keep the count in.

Understanding the scope of the problem is the first step on the path to true panic
Darryl Burke

Joined: May 03, 2008
Posts: 5115

You need to use a java.util.Map

luck, db
There are no new questions, but there may be new answers.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

You will need to build the data. What the above folks are suggesting is as follows...

define a map with a String as the key, and an Int as the value.

read through your array, one at a time.

For each element, check the map to see if the current element exists as a key

If not, add it in with '1' as the value

If so, increment the counter that currently exists.

When you have gone through the array list, you can get a list of all the keys, and then use that to get all the values.


If your input will definitely be nothing more than single characters, you can accomplish the same thing with a 26 element array of ints.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: Array List
It's not a secret anymore!