Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

How to correct this code to filter only when orgId also matches within loop

 
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to filter employees where age is more than the ageCriteria for the organization. How do I loop the employeeCriteria inside the filter and apply age filter where the organizationId also matches? Below is the code I have tried ,which requires correction in the line with filter condition:



Thanks
 
Master Rancher
Posts: 4701
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would really help it if you got the indentation right as that is really difficult to follow like it is.
 
Dave Tolls
Master Rancher
Posts: 4701
49
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't it be simpler to use a Map of organisation id to criteria?
Though you would need to use the same values, as currently your capitalisation is different for the organisation. ("Org1" vs "org1").
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:It would really help it if you got the indentation right as that is really difficult to follow like it is.



Thanks. I corrected the indentation and edited.
 
Sheriff
Posts: 15942
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you get the right code, how do you know it actually works with the data you supplied? None of the data will get filtered out. I would include some employees that I would expect to get filtered out of the results.

Edit: Actually, to Dave's point, unless you ignore case for the org name all of the data you have will get filtered out.

The point is that you should have both  data that will be expected to be included in the result and data that will be expected to be excluded.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I have now updated the test data and also have have corrected the org id case mismatch . I believe now it should surely return filtered rows for each org id once the program runs correctly .
 
Dave Tolls
Master Rancher
Posts: 4701
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what does it now do?
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to apply loop inside the filter but did not know how to do it. But ,now I am thinking that should it be the other way round.i,e filter inside the loop instead or loop inside filter. I will try this.
 
Saloon Keeper
Posts: 22664
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You seem to have the essential code commented out, so it's hard to tell what's going on. But I'm not sure about explicitly coding a loop anyway. Others will have to correct me, but at least in some cases, I'd expect the paradigm to be invariant of the inspection method.

Or, in plain English, whether the actions applied were done via a loop or via a hypothetical process that did them all in parallel should not be important.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote: But I'm not sure about explicitly coding a loop anyway. .



Thanks. Wouldn't loop be required if we have to check each element for organization ID match ? If not loop then what would be the other way of doing it ?
 
Tim Holloway
Saloon Keeper
Posts: 22664
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:

Tim Holloway wrote: But I'm not sure about explicitly coding a loop anyway. .



Thanks. Wouldn't loop be required if we have to check each element for organization ID match ? If not loop then what would be the other way of doing it ?



Given the right processor and environment, you could match in parallel. Like if you had a mix of beans and tomatoes and a sieve with holes that can pass beans but not tomatoes. You dump it all in and only the beans fall out. Today's GPUs can often run over 1000 operations in parallel, so if an operation could use the GPU, it would be a waste of time and resources to loop sequentially. By removing an explicit loop and defining the operation more abstractly you allow the system to make better choices on how to do what you want.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the below code but it gave me the below error:

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed

This comes at the line where filter condition is there.

 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Wouldn't it be simpler to use a Map of organisation id to criteria?
Though you would need to use the same values, as currently your capitalisation is different for the organisation. ("Org1" vs "org1").


Thanks .I think this may solve the issue caused by using a loop which gives "stream already closed ". I will try this way.

 
Marshal
Posts: 70672
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . using a loop which gives "stream already closed ". . . .

Has nobody ever told you only to use each Stream object once? Don't try mixing loops and Streams.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I tried the below code but it gave me the below error:

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed

This comes at the line where filter condition is there.



Sorry .I don't know how by mistake I got the corrected code reverted to old code with capital case OrgId instead of orgId and no valid test results.
Moreover ,the approach of loop was wrong .
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Wouldn't it be simpler to use a Map of organisation id to criteria?
Though you would need to use the same values, as currently your capitalisation is different for the organisation. ("Org1" vs "org1").



Thanks a lot. The accurate suggestion resolved the issue.

Also, I was startled to see that how easy the code was after using Map instead of Loop(which was ofcourse incorrect )

Below is the working code :


 
Marshal
Posts: 25960
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's much closer to good code. Just a couple of things:

1. Your Map should be a Map<String, Integer> so that you don't need to do any casting. And also just because using generics is better programming, since it makes it much harder to have casting issues.

2. You don't need two Streams. Just run the code together like this:


 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.I understood how through generics one can avoid error prone casting by using  the typecasting of generics.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above case the condition is ( age > ageCriteria).
Suppose we have N number of conditions such as param > paramCriteria
Where param can be age ,salary etc and criteria can be a number.

Given that a stream can be operated only once and thus we can't apply a loop, how can one apply filter on stream for N number of conditions.
 
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a separate map for all the conditions (not only for age), then you can do:
.
Here is an example that has two predicates and splits the employees into four groups: those who satisfy both predicates, one predicate and no predicate.


 
Master Rancher
Posts: 3701
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, Piet assumes you want some sort of combination of different filter results, hence his classification() method.  I, meanwhile, am guessing you might just be interested in what Employees pass all the conditions.

Monica Shiralkar wrote:Given that a stream can be operated only once and thus we can't apply a loop, how can one apply filter on stream for N number of conditions.


Well, using streams doesn't mean you have to use streams for everything.  You can have some things in a Collection of some sort that allows you to stream repeatedly.  In this case, you presumably have a large number of employees and a much smaller number of conditions - I would probably put the conditions in a List, and leave the employees in a Stream. That lets us replay the conditions many times, but visit each Employee only once.

 
Paul Clapham
Marshal
Posts: 25960
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A simpler method: say you have two conditions. You could write



Or perhaps that's too much code and it's hard to read. Then you could write



You have three conditions? Same sort of thing. Filter on them all at once or one at a time.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:  I, meanwhile, am guessing you might just be interested in what Employees pass all the conditions



No. This would mean doing an AND between conditions and applying the filter.I neither want AND not want OR.

If 3 conditions are there and 2 passes applying an AND or OR will result in 1 condition to pass through filter.
Instead I would want 2 successful conditions to pass through the filter .

Sorry I didn't explain this properly earlier.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all.I will try the suggestions given for my case.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:A simpler method: say you have two conditions. You could write



Or perhaps that's too much code and it's hard to read. Then you could write



You have three conditions? Same sort of thing. Filter on them all at once or one at a time.




If 3 conditions are there and 2 passes the filter ,this would result in 1 output of the filter whereas I would want 2 output of the filter for the 2 successful conditions.
 
Piet Souris
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:Hm, Piet assumes you want some sort of combination of different filter results (...)


I assume nothing. My first example uses the Predicates default method 'and' where you are using the 'allMatch'. The second code snippet is just an example.
 
Piet Souris
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is still not clear to me what you mean. You write:

"If 3 conditions are there and 2 passes the filter"

You mean two employees that meet the three conditions, or employees that meet two of the three conditions?

"whereas I would want 2 output of the filter for the 2 successful conditions".

Can you explain what you mean here?
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:It is still not clear to me what you mean. You write:

"If 3 conditions are there and 2 passes the filter"

You mean two employees that meet the three conditions, or employees that meet two of the three conditions?

"whereas I would want 2 output of the filter for the 2 successful conditions".

Can you explain what you mean here?




Thanks.Suppose the output of Filter is a print statement saying say the filter condition has passed.

If ageCondition for employee is 30 and salaryCondition is 10000.

If age is 25 and salary 9000, the No output should be printed obviously.

But if age of employee is 32 and salary 11000,then I would not want 1 output like the Filter condition has passed.
Instead ,I would want 2 output print statements one saying say "age condition has passed" and the second saying "salary condition has passed ".

If value for age is 32 and value for salary is 9000, it should print 1 output saying "age condition has passed ".
 
Piet Souris
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see, that is a nice one! First thing that came up to me:

Suppose you have the Predicates p1, p2,... and you create a Map<Predicate, String>.

And then you have this method:

You could then have this code:

But there are probably easier methods thinkable!
 
Campbell Ritchie
Marshal
Posts: 70672
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I understand: you have three tests you want to apply and you want the program to print out which objects pass ≥ 2 of those tests.

I think Mike Simmonds is correct

. . . using streams doesn't mean you have to use streams for everything. . . .

And shall we try creating an object to encapsulate your tests? What about an EmployeeTest class. Pass the employee to its constructor and an int (=tests to pass) and a Predicate... to a method which count how many tests you need to pass. You will need all sorts of checks for the int to validate that its value is appropriate for the size of the array.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not sure I understand: you have three tests you want to apply and you want the program to print out which objects pass ≥ 2 of those tests.



No. I instead meant like below :

Suppose the output of Filter is a print statement saying say the filter condition has passed.

If ageCondition for employee is 30 and salaryCondition is 10000.

If age is 25 and salary 9000, the No output should be printed obviously.

But if age of employee is 32 and salary 11000,then I would not want 1 output like the Filter condition has passed.
Instead ,I would want 2 output print statements one saying say "age condition has passed" and the second saying "salary condition has passed ".

If value for age is 32 and value for salary is 9000, it should print 1 output saying "age condition has passed ".
 
Campbell Ritchie
Marshal
Posts: 70672
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
32 and 10000 aren't conditions. Do you want people with age or salary above those numbers?
 
Paul Clapham
Marshal
Posts: 25960
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Instead ,I would want 2 output print statements one saying say "age condition has passed" and the second saying "salary condition has passed ".



Well then, use forEach() and call some code which has those two if-statements in it. You seem to be going out of your way to make this stuff difficult.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:32 and 10000 aren't conditions. Do you want people with age or salary above those numbers?



Yes
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The map approach solved the problem of picking only for orgId with key of map being the orgId. The value will be list of conditions.

Now the problem which is remains rcome down to "applying a filter to a list of conditions".
 
Piet Souris
Bartender
Posts: 4107
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you see, we have been translating those maps with its value into Predicates. So, what is a 'list of conditions' and how can the Predicates be determined from that? Please, give a really comprehensive example.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List of conditions means like first condition denotes age greater than age criteria ,the second denotes salary greater than salary criteria and so on.

 
Paul Clapham
Marshal
Posts: 25960
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why not just use some boolean logic to combine all of this list into one (complex) condition?

Like "(age > 35) and (salary < 20000)", for example. Or "(age > 45) or (salary >3000)". It should be possible to do that.
 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think below way of doing will work fine :

Instead of applying filter on the stream do the below

For each row in the stream , fetch it's conditions and combine to form set of objects.

Employee1Data-employee1condition1
Employee1Data-employee1condition2
Employee2Data-employee2condition1
Employee2Data-employee2condition2
Employee2Data-employee2condition3

And so on

Create another stream with such objects.

So in the above example the first stream had 2 rows but the second stream has 5.

Now instead of applying filter on the original stream , apply filter on this new stream.

 
Monica Shiralkar
Ranch Foreman
Posts: 1804
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:So why not just use some boolean logic to combine all of this list into one (complex) condition?

Like "(age > 35) and (salary < 20000)", for example. Or "(age > 45) or (salary >3000)". It should be possible to do that.



Using AND/OR would give lesser number of outputs.
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic