Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collection

 
vivek shankare gowda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use Map<identity, count++>
 
vivek shankare gowda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please elaborate the code..
 
Matthew Brown
Bartender
Posts: 4565
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12090
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20774
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please explain the steps to know how often those distinct entries appear in the list..
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply..first i will go through those tutorials..
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vivek shankare gowda wrote:Thank you for the reply..first i will go through those tutorials..

And then read the StopCoding page.

Winston
 
vivek shankare gowda
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
awesome page.. thanks all again for wonderful tips and suggestions
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic