0

In the below code I am iterating set And here i need to get one object from the set where primary deparment is true and status is active how i can reduce the code to execute in one step.

Set<EmployeeDeparment> dep = p.getRequestEmpId().getEmpdep();       
if (!dep.isEmpty()) {
    for (EmployeeDeparment employeeDeparment : dep) {
        if (employeeDeparment.isPrimarydept() == true && employeeDeparment.isStatus() == true) {
            System.out.println("inside the employeedep");
            leaveRequestForm.setApprovalauthority(employeeDeparment.getReportingTo().getFirstname() + "" + employeeDeparment.getReportingTo().getLastname());
        }
    }
} else {
     leaveRequestForm.setApprovalauthority("Approvalauthority not Configured");
}

Updated

List<EmployeeDeparment> beerDrinkers = select(persons, having(on(EmployeeDeparment.class).isPrimarydept(),
                        true));

This is giving compile time error.

simo.3792
  • 2,102
  • 1
  • 17
  • 29
user3108190
  • 27
  • 1
  • 2
  • 10
  • Your question has been answered already - http://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection – Deepak Bala Jul 21 '14 at 08:00
  • List beerDrinkers = select(persons, having(on(EmployeeDeparment.class).isPrimarydept(), true)); I have written like this but i am getting exception as create on class – user3108190 Jul 21 '14 at 09:03
  • What version of Java are you using? Java 6, Java 7 or Java 8? – EpicPandaForce Jul 21 '14 at 09:16

2 Answers2

0

You can use Guava's Iterables.filter() method.

You're predicate will be something like this:

Predicate<EmployeeDeparment> predicate = new Predicate<EmployeeDeparment>() {

public boolean apply(EmployeeDeparment employeeDeparment){
    return employeeDeparment.isPrimarydept() == true && employeeDeparment.isStatus() == true;
}

Your loop will then be:

for (EmployeeDeparment filteredEmployeeDeparment : Iterables.filter(dep, predicate)) {
    ....
}
Fabien Fleureau
  • 693
  • 5
  • 14
0
Set<EmployeeDeparment> dep = p.getRequestEmpId().getEmpdep();       
if (!dep.isEmpty()) 
{
    for (EmployeeDeparment employeeDeparment : dep) 
    {
       if (employeeDeparment.isPrimarydept() == true && employeeDeparment.isStatus() == true) 
       {
           System.out.println("inside the employeedep");
           leaveRequestForm.setApprovalauthority(employeeDeparment.getReportingTo().getFirstname() + "" + employeeDeparment.getReportingTo().getLastname());
       }
    }
}
else 
{
    leaveRequestForm.setApprovalauthority("Approvalauthority not Configured");
}

can be replaced by

Set<EmployeeDeparment> dep = p.getRequestEmpId().getEmpdep();       
if (!dep.isEmpty()) 
{
    dep.stream()
       .filter((employeeDeparment) -> employeeDeparment.isPrimarydept() == true && employeeDeparment.isStatus() == true)
       .forEach((x) -> 
       {
           System.out.println("inside the employeedep");
           leaveRequestForm.setApprovalauthority(employeeDeparment.getReportingTo().getFirstname() + "" + employeeDeparment.getReportingTo().getLastname());
       });
}
else
{
    leaveRequestForm.setApprovalauthority("Approvalauthority not Configured");
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • if anyone knows a better way of handling the two conditions though, I'm all ears! – EpicPandaForce Jul 21 '14 at 11:23
  • 1
    the OP has the second part outside of the loop, as else to **"if (!dep.isEmpty())"** – simo.3792 Jul 22 '14 at 05:19
  • @simo.3792095 thank you for the observation, K&R tends to mess me up -_- fixed and thanks again – EpicPandaForce Jul 22 '14 at 08:29
  • You've made me look up K&R now. I was oblivious to the fact that each positioning of the open curly brace and indenting had a different style name (thanks wikipedia). That aside the original post didn't follow any of those particular styles, so that explains the confusion. – simo.3792 Jul 23 '14 at 01:41
  • @simo.3792095 yeah it does, some of them are pretty weird. But I think Allman style with 4 space indent is the best, but at work I am forced to use k&r 2 space indent - it is so jarbled! The control keywords are over the statements and the whole thing is a mess, the opening braces are unpredictable, the code lacks structure, the blocks are non-existent. I really don't know why people still use it, as it makes debugging hell. – EpicPandaForce Jul 23 '14 at 07:34
  • Or at least that is the way I personally see it. Especially since I have fixed programming errors in someone else's code by using the Auto-formatter before. – EpicPandaForce Jul 23 '14 at 08:07