Creating 3 sublists based on unique conditions











up vote
0
down vote

favorite
1












I have written the following code for one of my requirements. Kindly help me if I can write this logic in a better.



public class PersonService {
public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
List<Person> adultList = allPersonFromDb.stream().filter(PersonUtil::isAdult).collect(Collectors.toList());
List<Person> underAgedList = allPersonFromDb.stream().filter(PersonUtil::isUnderAged).collect(Collectors.toList());
List<Person> seniorCitizenList = allPersonFromDb.stream().filter(PersonUtil::isSeniorCitizen).collect(Collectors.toList());

return new PersonCategorized(underAgedList, adultList, seniorCitizenList);
}
}

public class PersonCategorized {
private List<Person> underAgedPersonList;
private List<Person> adultList;
private List<Person> seniorCitizenList;

public PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
this.underAgedPersonList = underAgedPersonList;
this.adultList = adultList;
this.seniorCitizenList = seniorCitizenList;
}

public List<Person> getAdultList() {
return adultList;
}

public List<Person> getSeniorCitizenList() {
return seniorCitizenList;
}

public List<Person> getUnderAgedPersonList() {
return underAgedPersonList;
}
}

public class Person {
private int age;
private String name;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}
}

public class PersonUtil {
public static boolean isAdult(Person person) {
return person.getAge() >= 18 && person.getAge() < 60;
}

public static boolean isUnderAged(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}

public static boolean isSeniorCitizen(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}
}


One observation I had after posting here is I am iterating over the list 3 times to collect 3 different lists.










share|improve this question






















  • The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
    – akshaya pandey
    12 hours ago















up vote
0
down vote

favorite
1












I have written the following code for one of my requirements. Kindly help me if I can write this logic in a better.



public class PersonService {
public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
List<Person> adultList = allPersonFromDb.stream().filter(PersonUtil::isAdult).collect(Collectors.toList());
List<Person> underAgedList = allPersonFromDb.stream().filter(PersonUtil::isUnderAged).collect(Collectors.toList());
List<Person> seniorCitizenList = allPersonFromDb.stream().filter(PersonUtil::isSeniorCitizen).collect(Collectors.toList());

return new PersonCategorized(underAgedList, adultList, seniorCitizenList);
}
}

public class PersonCategorized {
private List<Person> underAgedPersonList;
private List<Person> adultList;
private List<Person> seniorCitizenList;

public PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
this.underAgedPersonList = underAgedPersonList;
this.adultList = adultList;
this.seniorCitizenList = seniorCitizenList;
}

public List<Person> getAdultList() {
return adultList;
}

public List<Person> getSeniorCitizenList() {
return seniorCitizenList;
}

public List<Person> getUnderAgedPersonList() {
return underAgedPersonList;
}
}

public class Person {
private int age;
private String name;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}
}

public class PersonUtil {
public static boolean isAdult(Person person) {
return person.getAge() >= 18 && person.getAge() < 60;
}

public static boolean isUnderAged(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}

public static boolean isSeniorCitizen(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}
}


One observation I had after posting here is I am iterating over the list 3 times to collect 3 different lists.










share|improve this question






















  • The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
    – akshaya pandey
    12 hours ago













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I have written the following code for one of my requirements. Kindly help me if I can write this logic in a better.



public class PersonService {
public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
List<Person> adultList = allPersonFromDb.stream().filter(PersonUtil::isAdult).collect(Collectors.toList());
List<Person> underAgedList = allPersonFromDb.stream().filter(PersonUtil::isUnderAged).collect(Collectors.toList());
List<Person> seniorCitizenList = allPersonFromDb.stream().filter(PersonUtil::isSeniorCitizen).collect(Collectors.toList());

return new PersonCategorized(underAgedList, adultList, seniorCitizenList);
}
}

public class PersonCategorized {
private List<Person> underAgedPersonList;
private List<Person> adultList;
private List<Person> seniorCitizenList;

public PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
this.underAgedPersonList = underAgedPersonList;
this.adultList = adultList;
this.seniorCitizenList = seniorCitizenList;
}

public List<Person> getAdultList() {
return adultList;
}

public List<Person> getSeniorCitizenList() {
return seniorCitizenList;
}

public List<Person> getUnderAgedPersonList() {
return underAgedPersonList;
}
}

public class Person {
private int age;
private String name;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}
}

public class PersonUtil {
public static boolean isAdult(Person person) {
return person.getAge() >= 18 && person.getAge() < 60;
}

public static boolean isUnderAged(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}

public static boolean isSeniorCitizen(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}
}


One observation I had after posting here is I am iterating over the list 3 times to collect 3 different lists.










share|improve this question













I have written the following code for one of my requirements. Kindly help me if I can write this logic in a better.



public class PersonService {
public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
List<Person> adultList = allPersonFromDb.stream().filter(PersonUtil::isAdult).collect(Collectors.toList());
List<Person> underAgedList = allPersonFromDb.stream().filter(PersonUtil::isUnderAged).collect(Collectors.toList());
List<Person> seniorCitizenList = allPersonFromDb.stream().filter(PersonUtil::isSeniorCitizen).collect(Collectors.toList());

return new PersonCategorized(underAgedList, adultList, seniorCitizenList);
}
}

public class PersonCategorized {
private List<Person> underAgedPersonList;
private List<Person> adultList;
private List<Person> seniorCitizenList;

public PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
this.underAgedPersonList = underAgedPersonList;
this.adultList = adultList;
this.seniorCitizenList = seniorCitizenList;
}

public List<Person> getAdultList() {
return adultList;
}

public List<Person> getSeniorCitizenList() {
return seniorCitizenList;
}

public List<Person> getUnderAgedPersonList() {
return underAgedPersonList;
}
}

public class Person {
private int age;
private String name;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public int getAge() {
return age;
}

public String getName() {
return name;
}
}

public class PersonUtil {
public static boolean isAdult(Person person) {
return person.getAge() >= 18 && person.getAge() < 60;
}

public static boolean isUnderAged(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}

public static boolean isSeniorCitizen(Person person) {
return person.getAge() >= 0 && person.getAge() < 18;
}
}


One observation I had after posting here is I am iterating over the list 3 times to collect 3 different lists.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 21 hours ago









NewUser

1154




1154












  • The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
    – akshaya pandey
    12 hours ago


















  • The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
    – akshaya pandey
    12 hours ago
















The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
– akshaya pandey
12 hours ago




The code is good and i donot believe in nitpicking. But the logic for senior citizen is incorrect. You can simply do public static boolean isSeniorCitizen(Person person) { return person.getAge() > =60; }
– akshaya pandey
12 hours ago










3 Answers
3






active

oldest

votes

















up vote
0
down vote













No much room for improvement here. The code is clear, readable, maintainable, short and nice. You could get rid of the three iterations by using a loop and filling three lists all in one iteration, but you'd sacrifice maintainability for a few microseconds saved. Don't do this unless you are in a real critical spot. For real world applications, keep it as it is.



One thing however is the PersonUtil. From an OO perspective, the Person should probably know whether it is underaged or senior or whatever and contain that business logic itself, so that you don't have to resort to a util-class. However, if the Person is an entity, eventually generated, you basically have no other choice.






share|improve this answer




























    up vote
    0
    down vote













    Always remember to use the access modifiers judiciously. From the question as the package hierarchy is unknown, I assumed that the all the classes are in the same package.



    public class PersonUtil {
    public static boolean isAdult(Person person) {
    return person.getAge() >= 18 && person.getAge() < 60;
    }

    public static boolean isUnderAged(Person person) {
    return person.getAge() >= 0 && person.getAge() < 18;
    }

    public static boolean isSeniorCitizen(Person person) {
    return person.getAge() >= 0 && person.getAge() < 18;
    }
    }

    public class Person {
    private String name;
    private int age;

    Person(String name, int age) {
    this.name = name;
    this.age = age;
    }

    public int getAge() {
    return age;
    }

    public String getName() {
    return name;
    }
    }

    public class PersonCategorized {
    private List<Person> underAgedPersonList;
    private List<Person> adultList;
    private List<Person> seniorCitizenList;

    PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
    this.underAgedPersonList = underAgedPersonList;
    this.adultList = adultList;
    this.seniorCitizenList = seniorCitizenList;
    }

    public List<Person> getAdultList() {
    return adultList;
    }

    public List<Person> getSeniorCitizenList() {
    return seniorCitizenList;
    }

    public List<Person> getUnderAgedPersonList() {
    return underAgedPersonList;
    }
    }



    public class PersonService {

    public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
    try (Stream<Person> personStream = allPersonFromDb.stream()) {
    return new PersonCategorized(
    collectFilteredList(personStream.filter(PersonUtil::isUnderAged)),
    collectFilteredList(personStream.filter(PersonUtil::isAdult)),
    collectFilteredList(personStream.filter(PersonUtil::isSeniorCitizen)));
    }
    }

    private List<Person> collectFilteredList(Stream<Person> filteredStream) {
    assert filteredStream == null;
    return filteredStream.collect(Collectors.toList());
    }

    }


    Naming for me seems to be not so okay check for singular and plural wherever required.






    share|improve this answer




























      up vote
      0
      down vote













      If conditions are exclusive you could use enum:



      class PersonService {

      public Map<PersonCategory, List<Person>> getCategorizedPerson(List<Person> allPersonFromDb) {
      return allPersonFromDb.stream().collect(Collectors.groupingBy(PersonUtil::getCategory));
      }
      }

      enum PersonCategory {
      UNDERAGE,
      SENIOR,
      DEFAULT
      }

      class Person {

      private int age;
      private String name;

      public Person(String name, int age) {
      this.name = name;
      this.age = age;
      }

      public int getAge() {
      return age;
      }

      public String getName() {
      return name;
      }
      }

      class PersonUtil {

      public static PersonCategory getCategory(Person person) {
      if (isUnderAged(person)) {
      return PersonCategory.UNDERAGE;
      }
      if (isSeniorCitizen(person)) {
      return PersonCategory.SENIOR;
      }
      return PersonCategory.DEFAULT;
      }

      public static boolean isUnderAged(Person person) {
      return person.getAge() >= 0 && person.getAge() < 18;
      }

      public static boolean isSeniorCitizen(Person person) {
      return person.getAge() >= 0 && person.getAge() < 18;
      }
      }





      share|improve this answer





















        Your Answer





        StackExchange.ifUsing("editor", function () {
        return StackExchange.using("mathjaxEditing", function () {
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
        });
        });
        }, "mathjax-editing");

        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "196"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208108%2fcreating-3-sublists-based-on-unique-conditions%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote













        No much room for improvement here. The code is clear, readable, maintainable, short and nice. You could get rid of the three iterations by using a loop and filling three lists all in one iteration, but you'd sacrifice maintainability for a few microseconds saved. Don't do this unless you are in a real critical spot. For real world applications, keep it as it is.



        One thing however is the PersonUtil. From an OO perspective, the Person should probably know whether it is underaged or senior or whatever and contain that business logic itself, so that you don't have to resort to a util-class. However, if the Person is an entity, eventually generated, you basically have no other choice.






        share|improve this answer

























          up vote
          0
          down vote













          No much room for improvement here. The code is clear, readable, maintainable, short and nice. You could get rid of the three iterations by using a loop and filling three lists all in one iteration, but you'd sacrifice maintainability for a few microseconds saved. Don't do this unless you are in a real critical spot. For real world applications, keep it as it is.



          One thing however is the PersonUtil. From an OO perspective, the Person should probably know whether it is underaged or senior or whatever and contain that business logic itself, so that you don't have to resort to a util-class. However, if the Person is an entity, eventually generated, you basically have no other choice.






          share|improve this answer























            up vote
            0
            down vote










            up vote
            0
            down vote









            No much room for improvement here. The code is clear, readable, maintainable, short and nice. You could get rid of the three iterations by using a loop and filling three lists all in one iteration, but you'd sacrifice maintainability for a few microseconds saved. Don't do this unless you are in a real critical spot. For real world applications, keep it as it is.



            One thing however is the PersonUtil. From an OO perspective, the Person should probably know whether it is underaged or senior or whatever and contain that business logic itself, so that you don't have to resort to a util-class. However, if the Person is an entity, eventually generated, you basically have no other choice.






            share|improve this answer












            No much room for improvement here. The code is clear, readable, maintainable, short and nice. You could get rid of the three iterations by using a loop and filling three lists all in one iteration, but you'd sacrifice maintainability for a few microseconds saved. Don't do this unless you are in a real critical spot. For real world applications, keep it as it is.



            One thing however is the PersonUtil. From an OO perspective, the Person should probably know whether it is underaged or senior or whatever and contain that business logic itself, so that you don't have to resort to a util-class. However, if the Person is an entity, eventually generated, you basically have no other choice.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 15 hours ago









            mtj

            2,814213




            2,814213
























                up vote
                0
                down vote













                Always remember to use the access modifiers judiciously. From the question as the package hierarchy is unknown, I assumed that the all the classes are in the same package.



                public class PersonUtil {
                public static boolean isAdult(Person person) {
                return person.getAge() >= 18 && person.getAge() < 60;
                }

                public static boolean isUnderAged(Person person) {
                return person.getAge() >= 0 && person.getAge() < 18;
                }

                public static boolean isSeniorCitizen(Person person) {
                return person.getAge() >= 0 && person.getAge() < 18;
                }
                }

                public class Person {
                private String name;
                private int age;

                Person(String name, int age) {
                this.name = name;
                this.age = age;
                }

                public int getAge() {
                return age;
                }

                public String getName() {
                return name;
                }
                }

                public class PersonCategorized {
                private List<Person> underAgedPersonList;
                private List<Person> adultList;
                private List<Person> seniorCitizenList;

                PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
                this.underAgedPersonList = underAgedPersonList;
                this.adultList = adultList;
                this.seniorCitizenList = seniorCitizenList;
                }

                public List<Person> getAdultList() {
                return adultList;
                }

                public List<Person> getSeniorCitizenList() {
                return seniorCitizenList;
                }

                public List<Person> getUnderAgedPersonList() {
                return underAgedPersonList;
                }
                }



                public class PersonService {

                public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
                try (Stream<Person> personStream = allPersonFromDb.stream()) {
                return new PersonCategorized(
                collectFilteredList(personStream.filter(PersonUtil::isUnderAged)),
                collectFilteredList(personStream.filter(PersonUtil::isAdult)),
                collectFilteredList(personStream.filter(PersonUtil::isSeniorCitizen)));
                }
                }

                private List<Person> collectFilteredList(Stream<Person> filteredStream) {
                assert filteredStream == null;
                return filteredStream.collect(Collectors.toList());
                }

                }


                Naming for me seems to be not so okay check for singular and plural wherever required.






                share|improve this answer

























                  up vote
                  0
                  down vote













                  Always remember to use the access modifiers judiciously. From the question as the package hierarchy is unknown, I assumed that the all the classes are in the same package.



                  public class PersonUtil {
                  public static boolean isAdult(Person person) {
                  return person.getAge() >= 18 && person.getAge() < 60;
                  }

                  public static boolean isUnderAged(Person person) {
                  return person.getAge() >= 0 && person.getAge() < 18;
                  }

                  public static boolean isSeniorCitizen(Person person) {
                  return person.getAge() >= 0 && person.getAge() < 18;
                  }
                  }

                  public class Person {
                  private String name;
                  private int age;

                  Person(String name, int age) {
                  this.name = name;
                  this.age = age;
                  }

                  public int getAge() {
                  return age;
                  }

                  public String getName() {
                  return name;
                  }
                  }

                  public class PersonCategorized {
                  private List<Person> underAgedPersonList;
                  private List<Person> adultList;
                  private List<Person> seniorCitizenList;

                  PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
                  this.underAgedPersonList = underAgedPersonList;
                  this.adultList = adultList;
                  this.seniorCitizenList = seniorCitizenList;
                  }

                  public List<Person> getAdultList() {
                  return adultList;
                  }

                  public List<Person> getSeniorCitizenList() {
                  return seniorCitizenList;
                  }

                  public List<Person> getUnderAgedPersonList() {
                  return underAgedPersonList;
                  }
                  }



                  public class PersonService {

                  public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
                  try (Stream<Person> personStream = allPersonFromDb.stream()) {
                  return new PersonCategorized(
                  collectFilteredList(personStream.filter(PersonUtil::isUnderAged)),
                  collectFilteredList(personStream.filter(PersonUtil::isAdult)),
                  collectFilteredList(personStream.filter(PersonUtil::isSeniorCitizen)));
                  }
                  }

                  private List<Person> collectFilteredList(Stream<Person> filteredStream) {
                  assert filteredStream == null;
                  return filteredStream.collect(Collectors.toList());
                  }

                  }


                  Naming for me seems to be not so okay check for singular and plural wherever required.






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Always remember to use the access modifiers judiciously. From the question as the package hierarchy is unknown, I assumed that the all the classes are in the same package.



                    public class PersonUtil {
                    public static boolean isAdult(Person person) {
                    return person.getAge() >= 18 && person.getAge() < 60;
                    }

                    public static boolean isUnderAged(Person person) {
                    return person.getAge() >= 0 && person.getAge() < 18;
                    }

                    public static boolean isSeniorCitizen(Person person) {
                    return person.getAge() >= 0 && person.getAge() < 18;
                    }
                    }

                    public class Person {
                    private String name;
                    private int age;

                    Person(String name, int age) {
                    this.name = name;
                    this.age = age;
                    }

                    public int getAge() {
                    return age;
                    }

                    public String getName() {
                    return name;
                    }
                    }

                    public class PersonCategorized {
                    private List<Person> underAgedPersonList;
                    private List<Person> adultList;
                    private List<Person> seniorCitizenList;

                    PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
                    this.underAgedPersonList = underAgedPersonList;
                    this.adultList = adultList;
                    this.seniorCitizenList = seniorCitizenList;
                    }

                    public List<Person> getAdultList() {
                    return adultList;
                    }

                    public List<Person> getSeniorCitizenList() {
                    return seniorCitizenList;
                    }

                    public List<Person> getUnderAgedPersonList() {
                    return underAgedPersonList;
                    }
                    }



                    public class PersonService {

                    public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
                    try (Stream<Person> personStream = allPersonFromDb.stream()) {
                    return new PersonCategorized(
                    collectFilteredList(personStream.filter(PersonUtil::isUnderAged)),
                    collectFilteredList(personStream.filter(PersonUtil::isAdult)),
                    collectFilteredList(personStream.filter(PersonUtil::isSeniorCitizen)));
                    }
                    }

                    private List<Person> collectFilteredList(Stream<Person> filteredStream) {
                    assert filteredStream == null;
                    return filteredStream.collect(Collectors.toList());
                    }

                    }


                    Naming for me seems to be not so okay check for singular and plural wherever required.






                    share|improve this answer












                    Always remember to use the access modifiers judiciously. From the question as the package hierarchy is unknown, I assumed that the all the classes are in the same package.



                    public class PersonUtil {
                    public static boolean isAdult(Person person) {
                    return person.getAge() >= 18 && person.getAge() < 60;
                    }

                    public static boolean isUnderAged(Person person) {
                    return person.getAge() >= 0 && person.getAge() < 18;
                    }

                    public static boolean isSeniorCitizen(Person person) {
                    return person.getAge() >= 0 && person.getAge() < 18;
                    }
                    }

                    public class Person {
                    private String name;
                    private int age;

                    Person(String name, int age) {
                    this.name = name;
                    this.age = age;
                    }

                    public int getAge() {
                    return age;
                    }

                    public String getName() {
                    return name;
                    }
                    }

                    public class PersonCategorized {
                    private List<Person> underAgedPersonList;
                    private List<Person> adultList;
                    private List<Person> seniorCitizenList;

                    PersonCategorized(List<Person> underAgedPersonList, List<Person> adultList, List<Person> seniorCitizenList) {
                    this.underAgedPersonList = underAgedPersonList;
                    this.adultList = adultList;
                    this.seniorCitizenList = seniorCitizenList;
                    }

                    public List<Person> getAdultList() {
                    return adultList;
                    }

                    public List<Person> getSeniorCitizenList() {
                    return seniorCitizenList;
                    }

                    public List<Person> getUnderAgedPersonList() {
                    return underAgedPersonList;
                    }
                    }



                    public class PersonService {

                    public PersonCategorized getCategorizedPerson(List<Person> allPersonFromDb) {
                    try (Stream<Person> personStream = allPersonFromDb.stream()) {
                    return new PersonCategorized(
                    collectFilteredList(personStream.filter(PersonUtil::isUnderAged)),
                    collectFilteredList(personStream.filter(PersonUtil::isAdult)),
                    collectFilteredList(personStream.filter(PersonUtil::isSeniorCitizen)));
                    }
                    }

                    private List<Person> collectFilteredList(Stream<Person> filteredStream) {
                    assert filteredStream == null;
                    return filteredStream.collect(Collectors.toList());
                    }

                    }


                    Naming for me seems to be not so okay check for singular and plural wherever required.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 15 hours ago









                    srk

                    2581511




                    2581511






















                        up vote
                        0
                        down vote













                        If conditions are exclusive you could use enum:



                        class PersonService {

                        public Map<PersonCategory, List<Person>> getCategorizedPerson(List<Person> allPersonFromDb) {
                        return allPersonFromDb.stream().collect(Collectors.groupingBy(PersonUtil::getCategory));
                        }
                        }

                        enum PersonCategory {
                        UNDERAGE,
                        SENIOR,
                        DEFAULT
                        }

                        class Person {

                        private int age;
                        private String name;

                        public Person(String name, int age) {
                        this.name = name;
                        this.age = age;
                        }

                        public int getAge() {
                        return age;
                        }

                        public String getName() {
                        return name;
                        }
                        }

                        class PersonUtil {

                        public static PersonCategory getCategory(Person person) {
                        if (isUnderAged(person)) {
                        return PersonCategory.UNDERAGE;
                        }
                        if (isSeniorCitizen(person)) {
                        return PersonCategory.SENIOR;
                        }
                        return PersonCategory.DEFAULT;
                        }

                        public static boolean isUnderAged(Person person) {
                        return person.getAge() >= 0 && person.getAge() < 18;
                        }

                        public static boolean isSeniorCitizen(Person person) {
                        return person.getAge() >= 0 && person.getAge() < 18;
                        }
                        }





                        share|improve this answer

























                          up vote
                          0
                          down vote













                          If conditions are exclusive you could use enum:



                          class PersonService {

                          public Map<PersonCategory, List<Person>> getCategorizedPerson(List<Person> allPersonFromDb) {
                          return allPersonFromDb.stream().collect(Collectors.groupingBy(PersonUtil::getCategory));
                          }
                          }

                          enum PersonCategory {
                          UNDERAGE,
                          SENIOR,
                          DEFAULT
                          }

                          class Person {

                          private int age;
                          private String name;

                          public Person(String name, int age) {
                          this.name = name;
                          this.age = age;
                          }

                          public int getAge() {
                          return age;
                          }

                          public String getName() {
                          return name;
                          }
                          }

                          class PersonUtil {

                          public static PersonCategory getCategory(Person person) {
                          if (isUnderAged(person)) {
                          return PersonCategory.UNDERAGE;
                          }
                          if (isSeniorCitizen(person)) {
                          return PersonCategory.SENIOR;
                          }
                          return PersonCategory.DEFAULT;
                          }

                          public static boolean isUnderAged(Person person) {
                          return person.getAge() >= 0 && person.getAge() < 18;
                          }

                          public static boolean isSeniorCitizen(Person person) {
                          return person.getAge() >= 0 && person.getAge() < 18;
                          }
                          }





                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            If conditions are exclusive you could use enum:



                            class PersonService {

                            public Map<PersonCategory, List<Person>> getCategorizedPerson(List<Person> allPersonFromDb) {
                            return allPersonFromDb.stream().collect(Collectors.groupingBy(PersonUtil::getCategory));
                            }
                            }

                            enum PersonCategory {
                            UNDERAGE,
                            SENIOR,
                            DEFAULT
                            }

                            class Person {

                            private int age;
                            private String name;

                            public Person(String name, int age) {
                            this.name = name;
                            this.age = age;
                            }

                            public int getAge() {
                            return age;
                            }

                            public String getName() {
                            return name;
                            }
                            }

                            class PersonUtil {

                            public static PersonCategory getCategory(Person person) {
                            if (isUnderAged(person)) {
                            return PersonCategory.UNDERAGE;
                            }
                            if (isSeniorCitizen(person)) {
                            return PersonCategory.SENIOR;
                            }
                            return PersonCategory.DEFAULT;
                            }

                            public static boolean isUnderAged(Person person) {
                            return person.getAge() >= 0 && person.getAge() < 18;
                            }

                            public static boolean isSeniorCitizen(Person person) {
                            return person.getAge() >= 0 && person.getAge() < 18;
                            }
                            }





                            share|improve this answer












                            If conditions are exclusive you could use enum:



                            class PersonService {

                            public Map<PersonCategory, List<Person>> getCategorizedPerson(List<Person> allPersonFromDb) {
                            return allPersonFromDb.stream().collect(Collectors.groupingBy(PersonUtil::getCategory));
                            }
                            }

                            enum PersonCategory {
                            UNDERAGE,
                            SENIOR,
                            DEFAULT
                            }

                            class Person {

                            private int age;
                            private String name;

                            public Person(String name, int age) {
                            this.name = name;
                            this.age = age;
                            }

                            public int getAge() {
                            return age;
                            }

                            public String getName() {
                            return name;
                            }
                            }

                            class PersonUtil {

                            public static PersonCategory getCategory(Person person) {
                            if (isUnderAged(person)) {
                            return PersonCategory.UNDERAGE;
                            }
                            if (isSeniorCitizen(person)) {
                            return PersonCategory.SENIOR;
                            }
                            return PersonCategory.DEFAULT;
                            }

                            public static boolean isUnderAged(Person person) {
                            return person.getAge() >= 0 && person.getAge() < 18;
                            }

                            public static boolean isSeniorCitizen(Person person) {
                            return person.getAge() >= 0 && person.getAge() < 18;
                            }
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 13 hours ago









                            user158037

                            52637




                            52637






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208108%2fcreating-3-sublists-based-on-unique-conditions%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin