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 collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "collection" Watch "collection" New topic
Author

collection

vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
if list has following elements 1,1,1,5,7,7,7,7,7,4,4... how to display the output as 1(3) 5(1) 7(5) 4(2).. here program is displaying only unique elements and counting the duplicate values
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

use Map<identity, count++>
vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
can you please elaborate the code..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Don't worry about the code to begin with. How would you do it by hand? Write down the steps you'd need to do to explain to someone else exactly how to do this. Once you've done that it's a lot simpler to convert into code. But until you understand how to solve it, this isn't a programming problem.
vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
as i understood we can use hashset object to avoid duplication

ArrayList list = new ArrayList(new HashSet(list));
if we use collections.frequency(list,1); it gives 1 number occurences..
is it a right approach??


fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

The idea is you write it down in English or any other natural language. You don't think in terms of java objects or collections. You think in terms of paper and pencil.

For example:

read each element, one by one.
each time i read one, look and see if it is written down on my piece of paper
if not, write it down on the next empty line
put a 'tick' mark next to the number (i.e. increase the count by one)
etc


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18655
    
    8

It's a good approach if you want to find the distinct entries from the list. It's not such a good approach if you want to know how often each of those distinct entries appears in the list.

So now go back to your original requirements and see if it's a good approach.
vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
can you please explain the steps to know how often those distinct entries appear in the list..
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
vivek shankare gowda wrote:can you please explain the steps to know how often those distinct entries appear in the list..


You've received solid advice that will lead you to the solution. Why are you ignoring it?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
We want to help you. At this site we feel that guiding someone to their own learning is a lot better than just giving them an answer. If the root of your difficulty is simply not knowing Java well enough to grapple with this problem, then it might be a good idea to work through some tutorials like the ones on Oracle's web site: http://docs.oracle.com/javase/tutorial/java/index.html and http://docs.oracle.com/javase/tutorial/collections/TOC.html
vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
Thank you for the reply..first i will go through those tutorials..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

vivek shankare gowda wrote:Thank you for the reply..first i will go through those tutorials..

And then read the StopCoding page.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
vivek shankare gowda
Ranch Hand

Joined: Jul 13, 2011
Posts: 35
awesome page.. thanks all again for wonderful tips and suggestions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: collection