Advertisement
iamaamir

Mannual VS Stream Filter

Mar 28th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  *
  6.  * @author toffe boy Aamir
  7.  */
  8. public class Filter {
  9.  
  10.     public static void main(String[] args) {
  11.  
  12.         List<Person> data = Person.CreateData();
  13. //Print Male Members with Manual Filter
  14.         System.out.println("MANUAL FILTER");
  15.         long startTime = System.nanoTime();
  16.         for (Person person : data) {
  17.             if (person.getGender() == Person.Sex.MALE) {
  18.                 System.out.println(person.getName());
  19.             }
  20.         }
  21.         long endTime = System.nanoTime();
  22.         long mannualFilterTime  = (endTime - startTime);
  23.         System.out.printf("Total Time %d\n", mannualFilterTime);
  24.  
  25. //Print Male Members with Stream Filter
  26.         System.out.println("\nSTREAM FILTER");
  27.         long startTime1 = System.nanoTime();
  28.         data.stream().
  29.                 filter(e -> e.getGender() == Person.Sex.MALE)
  30.                 .forEach(e -> System.out.println(e.getName()));
  31.         long endTime1 = System.nanoTime();
  32.         long streamFilterTime = (endTime1 - startTime1);
  33.         System.out.printf("Total Time %d\n", streamFilterTime);
  34.        
  35. //Who Wins
  36.         boolean decision = mannualFilterTime > streamFilterTime;
  37.         System.out.println((decision ? "Stream Filter Won the Match" : "Manual Filter Won the Match"));
  38.        
  39.        
  40.  
  41. ////Print Under Age Members
  42. //        System.out.println("\nUnder Age Members");
  43. //        data
  44. //                .stream()
  45. //                .filter(e -> e.getAge() < 18)
  46. //                .forEach(e -> System.out.println(e.getName()));
  47. //
  48. //        
  49. ////Print Average Age
  50. //        System.out.println("\nAverage Age");
  51. //        double avgAge = data
  52. //                .stream()
  53. //                .mapToInt(Person::getAge)
  54. //                .average()
  55. //                .getAsDouble();
  56. //        System.out.printf("Average age is %.0f\n", avgAge);
  57.  
  58.     }
  59.     }
  60.  
  61.     class Person {
  62.  
  63.         Person() {
  64.  
  65.         }
  66.  
  67.         public enum Sex {
  68.  
  69.             MALE, FEMALE
  70.         }
  71.  
  72.         private String name;
  73.         private Integer age;
  74.         private Sex gender;
  75.  
  76.         public Person(String Pname, int Page, Sex Pgender) {
  77.             this.name = Pname;
  78.             this.age = Page;
  79.             this.gender = Pgender;
  80.  
  81.         }
  82.  
  83.         public String getName() {
  84.             return name;
  85.         }
  86.  
  87.         public Integer getAge() {
  88.             return age;
  89.         }
  90.  
  91.         public Sex getGender() {
  92.             return gender;
  93.         }
  94.  
  95.         public static List<Person> CreateData() {
  96.             List<Person> info = new ArrayList<>();
  97.             info.add(new Person("Aamir khan", 19, Sex.MALE));
  98.             info.add(new Person("Tahir khan", 56, Sex.MALE));
  99.             info.add(new Person("Sana", 18, Sex.FEMALE));
  100.             info.add(new Person("Kahdija", 6, Sex.FEMALE));
  101.             info.add(new Person("Shahzeb Khan", 13, Sex.MALE));
  102.             info.add(new Person("Fatima", 1, Sex.FEMALE));
  103.  
  104.             return info;
  105.  
  106.         }
  107.  
  108.         public void ShowMembers() {
  109.             List<Person> Data = CreateData();
  110.             Data.stream().forEach((person) -> {
  111.                 System.out.println(person.getName().concat(" (".concat(person.getGender().toString().concat(")"))));
  112.             });
  113.  
  114.         }
  115.  
  116.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement