Monday, November 3, 2014

Java Best Pro



Calculate average age of male members

import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;
          public class Main 
          {
    public static void main(String[] args) 
    {
      List roster = createRoster();
      double average = roster
          .stream()
          .filter(p -> p.getGender() == Person.Sex.Male)
          .mapToInt(Person::getAge)
          .average()
          .getAsDouble();
          System.out.println("Average age (bulk data operations): " +average);
                     }
    public static List createRoster() 
    {
         List roster = new ArrayList<>();
        roster.add(
            new Person(
            "Fred",
            IsoChronology.Instance.date(1980, 6, 20),
            Person.Sex.Male,
            "fredexample"));
        roster.add(
            new Person(
            "Jane",
            IsoChronology.Instance.date(1990, 7, 15),
            Person.Sex.Female, "janeexample"));
        roster.add(
            new Person(
            "George",
            IsoChronology.Instance.date(1991, 8, 13),
            Person.Sex.Male, "georgeexample "));
        roster.add(
            new Person(
            "Bob",
            IsoChronology.Instance.date(2000, 9, 12),
            Person.Sex.Male, "bobexample"));
        return roster;
    }
}
class Person 
{
            public enum Sex 
            {
      Male, Female
  }
            String name; 
  LocalDate birthday;
  Sex gender;
  String emailAddress;
Person(String nameArg, LocalDate birthdayArg,Sex genderArg, String emailArg) 
 {
      name = nameArg;
      birthday = birthdayArg;
      gender = genderArg;
      emailAddress = emailArg;
  }  
public int getAge() 
{
      return birthday
          .until(IsoChronology.Instance.dateNow())
          .getYears();
 }
 public void printPerson() 
{
    System.out.println(name + ", " + this.getAge());
  }
  public Sex getGender() 
 {
      return gender;
  }
  public String getName() 
 {
      return name;
  }
  public String getEmailAddress() 
  {
      return emailAddress;
  }
  public LocalDate getBirthday() 
 {
      return birthday;
  }
  public static int compareByAge(Person a, Person b) 
 {
      return a.birthday.compareTo(b.birthday);
  }
}

Get Average age by gender

import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.IntConsumer;
import java.util.stream.Collectors;
           public class Main 
             {
    public static void main(String[] args) 
    {
      List roster = createRoster();
      
      
      System.out.println("Average age by gender:");
      Map averageAgeByGender =
          roster
              .stream()
              .collect(
                   Collectors.groupingBy(
                       Person::getGender,                      
                       Collectors.averagingInt(Person::getAge)));
         for (Map.Entry e : averageAgeByGender.entrySet()) 
        {
          System.out.println(e.getKey() + ": " + e.getValue());
      }
    }
              public static List createRoster() 
               {
        List roster = new ArrayList<>();
        roster.add(
            new Person(
            "Fred",
            IsoChronology.Instance.date(1980, 6, 20),
            Person.Sex.Male,
            "fredexample "));
        roster.add(
            new Person(
            "Jane",
            IsoChronology.Instance.date(1990, 7, 15),
            Person.Sex.Female, "janeexample"));
        roster.add(
            new Person(
            "George",
            IsoChronology.Instance.date(1991, 8, 13),
            Person.Sex.Male, "georgeexample "));
        roster.add(
            new Person(
            "Bob",
            IsoChronology.Instance.date(2000, 9, 12),
            Person.Sex.Male, "bobexample"));
        return roster;
    }
}
class Averager implements IntConsumer
{
    private int total = 0;
    private int count = 0;
 public double average() 
{
        return count > 0 ? ((double) total)/count : 0;
 }
 public void accept(int i) { total += i; count++; }
 public void combine(Averager other) 
 {
        total += other.total;
        count += other.count;
    }
}
class Person 
{
 public enum Sex 
 {
      Male, female
  }
            String name; 
  LocalDate birthday;
  Sex gender;
  String emailAddress;
        Person(String nameArg, LocalDate birthdayArg,Sex genderArg, String emailArg) 
{
      name = nameArg;
      birthday = birthdayArg;
      gender = genderArg;
      emailAddress = emailArg;
  }  
             public int getAge() 
             {
      return birthday
          .until(IsoChronology.Instance.dateNow())
          .getYears();
  public void printPerson() 
 {
    System.out.println(name + ", " + this.getAge());
  }
  public Sex getGender() 
 {
      return gender;
  }
  public String getName() 
  {
      return name;
  }
 public String getEmailAddress() 
 {
      return emailAddress;
  }
  public LocalDate getBirthday() 
  {
      return birthday;
   }
  public static int compareByAge(Person a, Person b) 
  {
      return a.birthday.compareTo(b.birthday);
  }
         }

No comments:

Post a Comment