• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

findFirst() alternative in stream API of java 8 to find all records

 
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have database execute method returning a resultMap with a key as rd and value as list of custom object Employee data.

Map<String,Object> resultMap=getEmpBatchDB.execute(paramMap) currently doing below operation emp=resultMap.get("rd")).findFirst().get(); It is only giving one object which is first record of emp only. Returning one causing defect.

Instead of returning one how to return all records. I wish there is method like findAll() similar to findFirst() on stream API but not there.

Please advise
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:I have database execute method returning a resultMap with a key as rd and value as list of custom object Employee data.


If in fact the value were a list of data, you'd already be done, right?  But apparently not...

sai rama krishna wrote:emp=resultMap.get("rd")).findFirst().get()


It looks like you've got an extra ")" in there, or a missing "(".  But, is it possible the thing returned by resultMap.get("rd") is a Stream of some sort, rather than a List?  In that case, you have a few options.  Given:

You can do:

or

or, if using JDK 16 or later:
 
sai rama krishna
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EmployeeData empData; //i am declaring at top of this dao class method called getEmpDataBatchDB
i have to return empData to calling service class
I am doing below 3 lines later on in same dao class method

}

i tried filter() and collect() as above.
Challenge is i have to return empData to calling service class method not ist<EmployeeData>. Please advise
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Challenge is i have to return empData to calling service class method not ist<EmployeeData>. Please advise


But that means you have to return ONE EmployeeData object. So writing code which produces more than one of them, even if that's what is needed in real life, isn't going to compile. You would have to change the method's return type if that's what is needed.
 
Mike Simmons
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us some code that will compile?  I see several errors in parentheses still that make it hard to be sure what you're trying to say.

sai rama krishna wrote:


Are you casting to List<Employee>?  That would look like this:

sai rama krishna wrote:


Sooo... what's happening here?  What class is get("rd") returning?  I thought you were casting it to List<Employee>, but List doesn't have a findFirst() method.  So what class are you working with now?  A List, a Stream, or something else?  Are you trying to cast it to something?

I also agree with Paul Clapham's question.  Do you want to return one employee (or EmployeeData or whatever), or a list/collection/array of many employees?  Can you change the return type of your method to a List<EmployeeData> or something similar?
 
sai rama krishna
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like i cannot change method signature so method still has to return EmployeeData object not List<EmployeeData>
 
sai rama krishna
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any traditional way i can achieve this without using stream filter collect java 8 stuff
 
Rancher
Posts: 1093
29
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Looks like i cannot change method signature so method still has to return EmployeeData object not List<EmployeeData>



sai,

I guess my question would be: "How constrained are you in the implementation?"  you say you must return an EmployeeData, but why?  do you have this with hundreds or even thousands of calls throughout your project?  is this a legacy project or a system that is outside of your rewrite authorization?  i guess: what is the reason that you are constrained to only return an EmployeeData record and not the List or some other data structure?

I have had some truly bitch of rewrites throughout my career, but I have always been given god rights in making any changes that I deem necessary to implement the desired features.

If you are truly constrained to return only 1 record, then you are constrained to return only 1 record (this is what you are telling us) ... any thought otherwise is nothing more than a fools errand because you are not authorized to do so by those that do have god powers.

If you do have the god power to change things as you need, then by all means--change it.  I've told this to more than one developer in my time: grow some stones and get the implementation done.  Even if it is a new feature and not a override of the existing call, make the requested changes to the project.

Les
 
sai rama krishna
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a developer I do not have god power


resulMap from database execute() method call has a key called rd using which i am getting value which is List<rd> object. rdList has field called information as below

private List<EmployeeInformationData> information;

So this EmployeeInformationData has String field empNumber which I have to set and return in the method.

So source is database execute call on the stored procedure
Target is EmployeeData with updated empNumber (if one set one, if multiple set multiple empNumbers) to rd object
Please advise
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about giving us sample code that is properly formatted and compiles. I see several things that are impossible in there.
 
Carey Brown
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sai rama krishna wrote:Looks like i cannot change method signature so method still has to return EmployeeData object not List<EmployeeData>

You have an impossible requirement. Period.
 
sai rama krishna
Ranch Hand
Posts: 933
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree. Most of the collection courses an YouTube videos i see they teach adding string value to collection object and retrieving and deleting etc simple things. Real world projects has these types of complex scenarios with agile methodologies people giving few hours and judging the performance. Any good courses or YouTube videos or books(not much time for reading these days) where these type of complex scenarios covered like map has list as value and that list is having other filed which is list type which has again string as empNumber string field to set and return that object. My mind some times goes blank and unable to wrap around these type of things to implement. Please advise
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic