Saturday, April 23, 2016

Data Set.

Data Set for Stream Interface's method understanding.

Now we will discuss the Stream Interface’s methods with examples. Before we do that we will setup some data for our use.

We will consider a Student data set which will comprise of Student’s first name, last name, address, grade, gender, age and favorite number.

Below are the classes that we will use. I have given the links of github for those classes too. For convenience I am putting code here too.
Github Links.
1.       Grade.java
2.       Name.java
3.       Gender.java
4.       Address.java
5.       Student.java


Grade.java code



public enum Grade {
       FRESHMAN, SOPHOMORE, JUNIOR, SENIOR;
}

Name.java

/**
 * Name class contains essential details for Name.
 * */
public final class Name implements Comparable<Name>{

       private final String firstName;
       private final String lastName;

       public Name(
                     final String firstName,
                     final String lastName) {
              this.firstName = firstName;
              this.lastName = lastName;
       }

       public String firstName() {
              return firstName;
       }

       public String lastName() {
              return lastName;
       }
      
       @Override
    public int compareTo(Name name) {
        int lastName = this.lastName().toLowerCase()
                .compareTo(name.lastName().toLowerCase());
        if(lastName == 0){
            int firstName = this.firstName().toLowerCase()
                    .compareTo(name.firstName().toLowerCase());
            if(firstName != 0){
                return firstName;
            }
        }
        return lastName;
    }
      
       /* (non-Javadoc)
        * @see java.lang.Object#hashCode()
        */
       @Override
       public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + ((firstName == null) ? 0 
                                                  : firstName.hashCode());
              result = prime * result + ((lastName == null) ? 0 
                                                  : lastName.hashCode());
              return result;
       }

       /* (non-Javadoc)
        * @see java.lang.Object#equals(java.lang.Object)
        */
       @Override
       public boolean equals(Object obj) {
              if (this == obj)
                     return true;
              if (obj == null)
                     return false;
              if (getClass() != obj.getClass())
                     return false;
              Name other = (Name) obj;
              if (firstName == null) {
                     if (other.firstName != null)
                           return false;
              } else if (!firstName.equals(other.firstName))
                     return false;
              if (lastName == null) {
                     if (other.lastName != null)
                           return false;
              } else if (!lastName.equals(other.lastName))
                     return false;
              return true;
       }

       @Override
       public String toString() {
             
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("lastName", this.lastName());
            map.put("firstName", this.firstName());
            return map.toString();
       }

}

Gender.java

/**
 * Gender will be used as Gender for
 * {@link Student}
 * */
public enum Gender {
       MALE, FEMALE, UNSPECIFIED;
}

Address.java

/**
 * Address class.
 * */
public final class Address {

     private final String address1;
     private final String address2;
     private final String pinCode;
     private final String city;
     private final String country;

     private Address(
                final AddressBuilder addressBuilder) {
           super();
           this.address1 = addressBuilder.address1;
           this.address2 = addressBuilder.address2;
           this.pinCode = addressBuilder.pinCode;
           this.city = addressBuilder.city;
           this.country = addressBuilder.country;
     }

     public static class AddressBuilder {

           private String address1;
           private String address2;
           private String pinCode;
           private String city;
           private String country;

           public AddressBuilder() {
           }

           public AddressBuilder address1(final String address1) {
                this.address1 = address1;
                return this;
           }

           public AddressBuilder address2(final String address2) {
                this.address2 = address2;
                return this;
           }

           public AddressBuilder pinCode(final String pinCode) {
                this.pinCode = pinCode;
                return this;
           }

           public AddressBuilder city(final String city) {
                this.city = city;
                return this;
           }

           public AddressBuilder country(final String country) {
                this.country = country;
                return this;
           }

           public Address build() {
                return new Address(this);
           }
     }

     public String address1() {
           return address1;
     }

     public String address2() {
           return address2;
     }

     public String pinCode() {
           return pinCode;
     }

     public String city() {
           return city;
     }

     public String country() {
           return country;
     }

     /* (non-Javadoc)
      * @see java.lang.Object#hashCode()
      */
     @Override
     public int hashCode() {
           final int prime = 31;
           int result = 1;
           result = prime * result + ((address1 == null) ? 0                                                : address1.hashCode());
           result = prime * result + ((address2 == null) ? 0                                                : address2.hashCode());
           result = prime * result + ((city == null) ? 0                                                    : city.hashCode());
           result = prime * result + ((country == null) ? 0                                                  : country.hashCode());
           result = prime * result + ((pinCode == null) ? 0                                                  : pinCode.hashCode());
           return result;
     }

     /* (non-Javadoc)
      * @see java.lang.Object#equals(java.lang.Object)
      */
     @Override
     public boolean equals(Object obj) {
           if (this == obj)
                return true;
           if (obj == null)
                return false;
           if (getClass() != obj.getClass())
                return false;
           Address other = (Address) obj;
           if (address1 == null) {
                if (other.address1 != null)
                     return false;
           } else if (!address1.equals(other.address1))
                return false;
           if (address2 == null) {
                if (other.address2 != null)
                     return false;
           } else if (!address2.equals(other.address2))
                return false;
           if (city == null) {
                if (other.city != null)
                     return false;
           } else if (!city.equals(other.city))
                return false;
           if (country == null) {
                if (other.country != null)
                     return false;
           } else if (!country.equals(other.country))
                return false;
           if (pinCode == null) {
                if (other.pinCode != null)
                     return false;
           } else if (!pinCode.equals(other.pinCode))
                return false;
           return true;
     }

     @Override
     public String toString() {

     Map<String, Object> map = new LinkedHashMap<String, Object>();
      map.put("address1", this.address1());
      map.put("address2", this.address2());
      map.put("pincode", this.pinCode());
      map.put("city", this.city());
      map.put("country", this.country());
      return map.toString();
     }

}

Student.java


/**
 * NOTE: There are no checks for <b>null</b>.
 * */
public final class Student implements Comparable<Student>{
      
       private final Name name;
       private final Address address;
       private final Integer age;
       private final Integer favoriteNumber;
       private final Gender gender;
       private final Grade grade;
      
       public Student(
                     final Name name,
                     final Address address,
                     final Integer age,
                     final Integer favoriteNumber,
                     final Gender gender,
                     final Grade grade) {
              this.name = name;
              this.address = address;
              this.age = age;
              this.favoriteNumber = favoriteNumber;
              this.gender = gender;
              this.grade = grade;
       }

       public Address address() {
              return address;
       }
      
       public Integer age() {
              return age;
       }
      
       public Integer favoriteNumber() {
              return favoriteNumber;
       }
      
       public Name name() {
              return name;
       }
      
       public Gender gender(){
              return gender;
       }
      
       public Grade grade(){
              return grade;
       }
      
       @Override
       public int compareTo(Student that) {
              return this.name.compareTo(that.name);
       }
      
       @Override
       public int hashCode() {
              final int prime = 31;
              int result = 1;
              result = prime * result + ((address == null) ? 0 
                                                    : address.hashCode());
              result = prime * result + ((name == null) ? 0 
                                                    : name.hashCode());
              return result;
       }

       @Override
       public boolean equals(Object obj) {
              if (this == obj)
                     return true;
              if (obj == null)
                     return false;
              if (getClass() != obj.getClass())
                     return false;
              Student other = (Student) obj;
              if (address == null) {
                     if (other.address != null)
                           return false;
              } else if (!address.equals(other.address))
                     return false;
              if (name == null) {
                     if (other.name != null)
                           return false;
              } else if (!name.equals(other.name))
                     return false;
              return true;
       }

       @Override
       public String toString(){
            Map<String, String> map = new LinkedHashMap<String, String>();
            map.put("studentName", this.name.toString());
            map.put("studentAddress", this.address.toString());
            map.put("studentAge", this.age.toString());
            map.put("favoriteNumber", String.valueOf(this.favoriteNumber));
            map.put("gender", this.gender.toString());
            map.put("grade", this.grade.toString());
            return map.toString();
       }
      
}

DataSet file.

public final class StudentDataSet {

       public static List<Student> dataSet(){
             
              List<Student> students = new ArrayList<Student>(10);
             
              Name name1 = new Name("John", "Doe");
              Address address1 = new Address.AddressBuilder()
                                            .address1("101 N Good Avenue")
                                            .address2("APT 1000")
                                            .city("NYC")
                                            .country("USA")
                                            .pinCode("49508")
                                            .build();
              Student student1 = new Student(name1, address1, Integer.valueOf(61), Integer.valueOf(564), Gender.MALE, Grade.SENIOR);
              students.add(student1);
             
              Name name2 = new Name("Jane", "Doe");
              Address address2 = new Address.AddressBuilder()
                                            .address1("101 N Good Avenue")
                                            .address2("APT 1000")
                                            .city("NYC")
                                            .country("USA")
                                            .pinCode("20200")
                                            .build();
              Student student2 = new Student(name2, address2, Integer.valueOf(52), 521, Gender.UNSPECIFIED, Grade.FRESHMAN);
              students.add(student2);
             
              Name name3 = new Name("Theresa", "Burton");
              Address address3 = new Address.AddressBuilder()
                                            .address1("1276 Neuport Lane")
                                            .address2("APT 10")
                                            .city("Atlanta")
                                            .country("USA")
                                            .pinCode("60605")
                                            .build();
              Student student3 = new Student(name3, address3, Integer.valueOf(15), 281, Gender.FEMALE, Grade.JUNIOR);
              students.add(student3);
             
              Name name4 = new Name("Basil", "Ballard");
              Address address4 = new Address.AddressBuilder()
                                       .address1("2660 Golden Ridge Road")
                                       .address2("APT 123")
                                       .city("Clifton Park")
                                       .country("Canada")
                                       .pinCode("48219")
                                       .build();
              Student student4 = new Student(name4, address4, Integer.valueOf(27), 228, Gender.MALE, Grade.SENIOR);
              students.add(student4);
             
              Name name5 = new Name("Tomas", "Cruz");
              Address address5 = new Address.AddressBuilder()
                                            .address1("4643 Goldie Lane")
                                            .address2("APT 324")
                                            .city("Dayton")
                                            .country("USA")
                                            .pinCode("23320")
                                            .build();
              Student student5 = new Student(name5, address5, Integer.valueOf(83), 454, Gender.UNSPECIFIED, Grade.JUNIOR);
              students.add(student5);
             
              Name name6 = new Name("Clyde", "Bell");
              Address address6 = new Address.AddressBuilder()
                                       .address1("3521 Trainer Avenue")
                                       .address2("APT 514")
                                       .city("North Henderson")
                                       .country("USA")
                                       .pinCode("39402")
                                       .build();
              Student student6 = new Student(name6, address6, Integer.valueOf(41), 715, Gender.FEMALE, Grade.SOPHOMORE);
              students.add(student6);
             
              Name name7 = new Name("Marcos", "Bell");
              Address address7 = new Address.AddressBuilder()
                                       .address1("3521 Trainer Avenue")
                                       .address2("APT 514")
                                       .city("North Henderson")
                                       .country("USA")
                                       .pinCode("21201")
                                       .build();
              Student student7 = new Student(name7, address7, Integer.valueOf(57), 527, Gender.MALE, Grade.FRESHMAN);
              students.add(student7);
             
              Name name8 = new Name("Michelle", "Scott");
              Address address8 = new Address.AddressBuilder()
                                            .address1("1579 Nixon Avenue")
                                            .address2("APT 1251")
                                            .city("NYC")
                                            .country("USA")  
                                            .pinCode("78401")
                                            .build();
              Student student8 = new Student(name8, address8, Integer.valueOf(16), 482, Gender.UNSPECIFIED, Grade.FRESHMAN);
              students.add(student8);
             
              Name name9 = new Name("Mary", "Derby");
              Address address9 = new Address.AddressBuilder()
                                     .address1("1803 Medical Center Drive")
                                     .address2("APT 438")
                                     .city("Tampa")
                                     .country("USA")
                                     .pinCode("02210")
                                     .build();
              Student student9 = new Student(name9, address9, Integer.valueOf(57), 101, Gender.FEMALE, Grade.SOPHOMORE);
              students.add(student9);
                    
              Name name10 = new Name("Margaret", "Towery");
              Address address10 = new Address.AddressBuilder()
                                     .address1("970 Amethyst Drive")
                                     .address2("APT 101")
                                     .city("Lansing")
                                     .country("USA")
                                     .pinCode("48933")
                                     .build();
              Student student10 = new Student(name10, address10, Integer.valueOf(17), 129, Gender.FEMALE, Grade.JUNIOR);
              students.add(student10);
             
              return Collections.unmodifiableList(students);
       }
}




Ads Inside Post