• Post Reply Bookmark Topic Watch Topic
  • New Topic

[SOLVED]Counting duplicates in ArrayList and sending to jsp page  RSS feed

 
Gunther Wils
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my servlet i want to count the duplicates in an ArrayListwhich contains objects gotten from a MySQL database
i was thinking of this:
- put the objects from the database in an arraylist
- check how many times one object is in that arraylist
- put the object once in another arraylist
- put the amount of times it comes in the arraylist in another arraylist
- send those 2 arraylists to the jsp page and show them in a for loop

so i came up with this code for my servlet(variable names are in Dutch)



but on my jsp page i get this as result (also in Dutch)



is there something i overlooked why this didn't work?
or is there another way to do it?
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you don't want an ArrayList.
why not use a Set and use the boolean return value of the add method to know if it already contains your value (Sets don't allow duplicates and add() returns false if the value already exists). If it already exists, you can increment the count in your other collection.

better yet, use a Map with your values as the keys and the counts as the value of the map. The keys of a Map are themselves a Set. Then you will only have to manage one Map vs three ArrayLists.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
something like this should work:


what is a "Keuze", anyway?
 
Gunther Wils
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"keuze" is Dutch for choice
the user should be able to add a choice to a database and i need to see how many times a choice has been chosen thats why i needed this

thanks a lot
 
Gunther Wils
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim McGuire wrote:something like this should work:


what is a "Keuze", anyway?


I tried it and i get an error on the first line saying:
wrong number of type arguments; requires 2
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gunther Wils wrote:
Tim McGuire wrote:something like this should work:


what is a "Keuze", anyway?


I tried it and i get an error on the first line saying:
wrong number of type arguments; requires 2


my mistake.
first line should be
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!