• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Why am I getting duplicate data using Stream?

 
Ranch Hand
Posts: 549
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello experts,

I hope someone can tell me what's wrong with the below code where I am trying to filter out the element in the List<string> which contains the data from the resultset.



It gives me a duplication so I tried to remove .collect(Collectors.toList()) but it is worse -  it is not showing anything and I am not sure in what way can I filter the elements that matches the resultSet...

Hope someone can tell me what's wrong with the above code or if there are other ways to achieve my goal.

Thanks.
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need some more context.
What type is subjs and what do you expect it to hold?
Where is your loop for rs.next()?
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an enhanced for() loop looping through subjs and you also use .stream() to loop through subjs. You are not using the data returned by toList().
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is just a wild guess because you've left a lot of information out of your post.
 
tangara goh
Ranch Hand
Posts: 549
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:This is just a wild guess because you've left a lot of information out of your post.



I did it the way as above but it is giving me the same result - duplications.

[UHigherPriEnglish]
[LSHigherMaths]
[UHigherPriEnglish]
[LSHigherMaths]

It is supposed to only give me just UHigherPriEnglish and LSHigherMaths.....

Kindly advise.
 
tangara goh
Ranch Hand
Posts: 549
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to add that this is not returning resultset on the subjects alone.  Above that, I have other resultsets which are giving me the right return of data.  Prior to doing the resultset, the preparedStatement already set the arrays.

Since I can't add the finding of the subjects to another List which is the resultSet of other data, I am separating out the resultsets of subjects and then display it separately as another list via set attributes.



 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:I did it the way as above but it is giving me the same result - duplications.

[UHigherPriEnglish]
[LSHigherMaths]
[UHigherPriEnglish]
[LSHigherMaths]

It is supposed to only give me just UHigherPriEnglish and LSHigherMaths.....


I would need 3 things:
  • A complete posting of your code with the new changes and how the method is being called and how the list is being used. Is it being called more than once?
  • A print of the contents of your subjs.
  • A dump of your database with the column_labels column.

  • The code as I showed it would not create duplicates unless one of these three had duplicates. Sprinkle some print calls into your code to see what is actually happening.
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    tangara goh
    Ranch Hand
    Posts: 549
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:



    HI Carey,

    I think the way I  was doing it not going to help me.

    The problem is I need a method to put the List<String> into List<tutor> so that it is easier for me to later display the resultset data in JSTL.

    So, could you let me know should I use a Map instead for I can't see how I can add List<String> to List<tutor>
     
    Carey Brown
    Bartender
    Posts: 6140
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:I would need 3 things:

  • A complete posting of your code with the new changes and how the method is being called and how the list is being used. Is it being called more than once?
  • A print of the contents of your subjs.
  • A dump of your database with the column_labels column.
  •  
    tangara goh
    Ranch Hand
    Posts: 549
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Carey Brown wrote:I would need 3 things:

  • A complete posting of your code with the new changes and how the method is being called and how the list is being used. Is it being called more than once?
  • A print of the contents of your subjs.
  • A dump of your database with the column_labels column.


  • The thing is that the method used is really wrong already.

    What I need now is really how to put a List<String> inside a List<Object> ....so that I can print out the resultset in rows in JSTL...

    cos Right now...the List<String> appear in one line instead of in rows....in JSTL

    So, that's the problem now.

     
    A "dutch baby" is not a baby. But this tiny ad is baby sized:
    professionally read, modify and write PDF files from Java
    https://products.aspose.com/pdf/java
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!