Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- /**
- *
- * @author toffe boy Aamir
- */
- public class Filter {
- public static void main(String[] args) {
- List<Person> data = Person.CreateData();
- //Print Male Members with Manual Filter
- System.out.println("MANUAL FILTER");
- long startTime = System.nanoTime();
- for (Person person : data) {
- if (person.getGender() == Person.Sex.MALE) {
- System.out.println(person.getName());
- }
- }
- long endTime = System.nanoTime();
- long mannualFilterTime = (endTime - startTime);
- System.out.printf("Total Time %d\n", mannualFilterTime);
- //Print Male Members with Stream Filter
- System.out.println("\nSTREAM FILTER");
- long startTime1 = System.nanoTime();
- data.stream().
- filter(e -> e.getGender() == Person.Sex.MALE)
- .forEach(e -> System.out.println(e.getName()));
- long endTime1 = System.nanoTime();
- long streamFilterTime = (endTime1 - startTime1);
- System.out.printf("Total Time %d\n", streamFilterTime);
- //Who Wins
- boolean decision = mannualFilterTime > streamFilterTime;
- System.out.println((decision ? "Stream Filter Won the Match" : "Manual Filter Won the Match"));
- ////Print Under Age Members
- // System.out.println("\nUnder Age Members");
- // data
- // .stream()
- // .filter(e -> e.getAge() < 18)
- // .forEach(e -> System.out.println(e.getName()));
- //
- //
- ////Print Average Age
- // System.out.println("\nAverage Age");
- // double avgAge = data
- // .stream()
- // .mapToInt(Person::getAge)
- // .average()
- // .getAsDouble();
- // System.out.printf("Average age is %.0f\n", avgAge);
- }
- }
- class Person {
- Person() {
- }
- public enum Sex {
- MALE, FEMALE
- }
- private String name;
- private Integer age;
- private Sex gender;
- public Person(String Pname, int Page, Sex Pgender) {
- this.name = Pname;
- this.age = Page;
- this.gender = Pgender;
- }
- public String getName() {
- return name;
- }
- public Integer getAge() {
- return age;
- }
- public Sex getGender() {
- return gender;
- }
- public static List<Person> CreateData() {
- List<Person> info = new ArrayList<>();
- info.add(new Person("Aamir khan", 19, Sex.MALE));
- info.add(new Person("Tahir khan", 56, Sex.MALE));
- info.add(new Person("Sana", 18, Sex.FEMALE));
- info.add(new Person("Kahdija", 6, Sex.FEMALE));
- info.add(new Person("Shahzeb Khan", 13, Sex.MALE));
- info.add(new Person("Fatima", 1, Sex.FEMALE));
- return info;
- }
- public void ShowMembers() {
- List<Person> Data = CreateData();
- Data.stream().forEach((person) -> {
- System.out.println(person.getName().concat(" (".concat(person.getGender().toString().concat(")"))));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement