[GO] Aggregate the results of the "favorite season" questionnaire, and also aggregate by gender and age

Table of Contents ⇒ Java Algorithm Library-Artery-Sample

Q01_00.java



package jp.avaj.lib.algo;

import java.util.ArrayList;
import java.util.List;

import jp.avaj.lib.def.ArCharDef;
import jp.avaj.lib.def.ArRepeat;
import jp.avaj.lib.test.L;

/**
 *Aggregate the results of the "favorite season" questionnaire, and also aggregate by gender and age
 *
 *・ Ask them to answer their favorite season.In addition, ask them to tell you their name, gender, and age..
 *・ Aggregate the results.
 *・ Aggregate by season.
 *・ Aggregate cases for women.
 *・ Aggregate by age group
 *
 *-Use the following methods of ArList.
 *    Integer[] histogram(Collection<T0> collection,ArCreator<T0,T1> creator,ArValidator<T1>[] validators)
 *-Create a T1 object from the T0 object with the creator and check it with validators..
 *-The count number corresponding to the validators array is returned..
 *
 *・ When there are multiple fields to be checked, such as "Aggregate by age group" ⇒ See the sample and read it again..
 *・ ArCreator Self(ArCreator which returns the argument itself)To use
 *-ArValidator that checks each field with T0 as an argument(Array of)To create.
 *-Create a combination of the above multiple ArValidator arrays.
 *・ A general-purpose ArValidator that supports T0(normally)ArValidator is an original implementation because it is impossible.
 *
 */
public class Q01_00 {
  public static void main(String[] args) throws Exception {
    L.p("Questionnaire");
    List<EnquetePaper> papers = execEnquete();
    //
    L.p("Aggregate by season");
    {
      //Creation of creator ⇒ The fields to be aggregated are seasonal
      ArCreator<EnquetePaper,ArSeason> creator = new ArCreator<EnquetePaper,ArSeason>() {
        @Override
        public ArSeason convert(EnquetePaper obj) throws Exception {
          return obj.getSeason();
        }
      };
      //Create an ArValidator for each season
      //ArValidatorEq determines if the check target is equal to the argument of the constructor ArValidator
      ArValidatorEq<ArSeason>[] validators = ArValidatorUtil.createArValidatorEqArray(ArSeason.values());
      //Tally
      Integer[] results = ArList.histogram(papers,creator,validators);
      //Take a look at the results
      L.p(ArObj.toString2Arrays(validators,results));
    }

    L.p("Aggregate by season-Another solution");
    {
      /*
       *Another solution that uses ArCreator Self.
       *Since the creator's return will be Enquete Paper, ArValidator will be an original implementation.
       *If there is only one field to check, it is not necessary to do this, so please see it as a sample..
       */
      //
      ArCreator<EnquetePaper,EnquetePaper> creator = new ArCreatorSelf<EnquetePaper>();
      //ArValidator to check the season
      class SeasonValidator implements ArValidator<EnquetePaper> {
        public SeasonValidator(ArSeason season) {
          this.season = season;
        }
        @Override
        public boolean check(EnquetePaper value) {
          return value.getSeason() == season;
        }
        @Override
        public String toString() {
          return ArObj.toString(season);
        }
        private ArSeason season;
      }
      //Create an ArValidator for each season
      ArValidator<EnquetePaper>[] validators = new ArValidator[] {
        new SeasonValidator(ArSeason.SPRING),
        new SeasonValidator(ArSeason.SUMMER),
        new SeasonValidator(ArSeason.AUTUMN),
        new SeasonValidator(ArSeason.WINTER),
      };
      //Tally
      Integer[] results = ArList.histogram(papers, creator, validators);
      //Take a look at the results
      L.p(ArObj.toString2Arrays(validators,results));
    }

    L.p("Aggregate cases for women");
    {
      //The fields to be aggregated are seasons
      ArCreator<EnquetePaper,ArSeason> creator = new ArCreator<EnquetePaper,ArSeason>() {
        @Override
        public ArSeason convert(EnquetePaper obj) throws Exception {
          //For men, return null ⇒ If null, it will not be counted(Can not).
          return obj.getGender()==ArGender.Female ? obj.getSeason() : null;
        }
      };
      //Create an ArValidator for each season
      ArValidator<ArSeason>[] validators = ArValidatorUtil.createArValidatorEqArray(ArSeason.values());
      //Tally
      Integer[] results = ArList.histogram(papers, creator, validators);
      //Take a look at the results
      L.p(ArObj.toString2Arrays(validators,results));
    }

    L.p("Aggregate cases for women-Another solution");
    {
      //The fields to be aggregated are seasons
      ArCreator<EnquetePaper,ArSeason> creator = new ArCreator<EnquetePaper,ArSeason>() {
        @Override
        public ArSeason convert(EnquetePaper obj) throws Exception {
          return obj.getSeason();
        }
      };
      //Create an ArValidator for each season
      ArValidator<ArSeason>[] validators = ArValidatorUtil.createArValidatorEqArray(ArSeason.values());
      //
      //Create data that extracts only women.
      List<EnquetePaper> femalePapers;
      {
        //Create an ArValidator that is true for female data
        ArValidator<EnquetePaper> femaleValidator = new ArValidator<EnquetePaper>() {
          @Override
          public boolean check(EnquetePaper value) {
            return value.getGender()==ArGender.Female;
          }
        };
        //Extract
        femalePapers = ArList.select(papers,femaleValidator);
      }
      //Aggregate for female Papers
      Integer[] results = ArList.histogram(femalePapers, creator, validators);
      //Take a look at the results
      L.p(ArObj.toString2Arrays(validators,results));
    }

    L.p("Aggregate by age group");
    {
      /*
       *The idea is to create an ArValidator that checks the age and an ArValidator that checks the season based on Enquete Paper..
       *ArValidator is an original implementation because EnquetePaper is a unique class and it is the field in it that is checked..
       *Two types of ArValidator(Array of)Based on the,(Using ArValidatorUtil)Create that combination.
       */
      //Use ArCreator Self to see age and seasonal fields
      ArCreator<EnquetePaper,EnquetePaper> creator = new ArCreatorSelf<EnquetePaper>();
      //Age ArValidator
      class AgeValidator implements ArValidator<EnquetePaper> {
        public AgeValidator(int low,int high) {
          this.low = low;
          this.high = high;
        }
        @Override
        public boolean check(EnquetePaper value) {
          return ((low <= value.getAge()) && (value.getAge() <= high));
        }
        @Override
        public String toString() {
          return low+"-"+high;
        }
        private int low;
        private int high;
      }
      //Divide age into early and late twenties
      ArValidator<EnquetePaper>[] validators0 = new ArValidator[] {
        new AgeValidator(20,24),
        new AgeValidator(25,29),
      };
      //Seasonal Ar Validator
      class SeasonValidator implements ArValidator<EnquetePaper> {
        public SeasonValidator(ArSeason season) {
          this.season = season;
        }
        @Override
        public boolean check(EnquetePaper value) {
          return season == value.getSeason();
        }
        @Override
        public String toString() {
          return ArObj.toString(season);
        }
        private ArSeason season;
      }
      //
      ArValidator<EnquetePaper>[] validators1 = new ArValidator[] {
        new SeasonValidator(ArSeason.SPRING),
        new SeasonValidator(ArSeason.SUMMER),
        new SeasonValidator(ArSeason.AUTUMN),
        new SeasonValidator(ArSeason.WINTER),
      };
      //Create a combination of age and seasonal ArValidator sequences
      //ArValidator Group is an ArValidator that combines multiple ArValidators.
      ArValidatorGroup<EnquetePaper>[] validators = ArValidatorUtil.createCombination(validators0,validators1);
      //Tally
      Integer[] results = ArList.histogram(papers, creator, validators);
      //Take a look at the results
      L.p(ArObj.toString2Arrays(validators,results));
    }
  }

  /**Conduct a questionnaire*/
  private static List<EnquetePaper> execEnquete() {
    /*
     *Note, RandomFrom below is an interface that randomly retrieves elements from a Collection or array..
     *You can specify whether to retrieve the same thing again with ArRepeat
     */
    //Name ⇒ Randomly generated from one uppercase letter ⇒ ArRepeat.NO(No same name)
    ArRandomFromArray<String> names = new ArRandomFromArray<String>(ArCharDef.upperStr,ArRepeat.NO);
    //Gender ⇒ Male/Either of the women(Ignore others)
    ArRandomFromArray<ArGender> genders = new ArRandomFromArray<ArGender>(new ArGender[]{ArGender.Male,ArGender.Female},ArRepeat.YES);
    //Age ⇒ 20 to 29 years old..
    ArRandomFromCollection<Integer> ages;
    {
      //Generate 20 to 10 numbers at intervals of 1
      List<Integer> values = ArList.constructSeqList(20,10,1);
      ages = new ArRandomFromCollection<Integer>(values,ArRepeat.YES); //There should be people of the same age..
    }
    //Season
    ArRandomFromArray<ArSeason> seansons = new ArRandomFromArray<ArSeason>(ArSeason.values(),ArRepeat.YES);
    //Questionnaire
    List<EnquetePaper> list = new ArrayList<EnquetePaper>();
    for (int i=0; i<20; i++) {
      EnquetePaper paper = new EnquetePaper(names.get(),genders.get(),ages.get(),seansons.get());
      //Output for visual confirmation ⇒ Works without redefining toString
      L.p(ArObj.toString(paper));
      list.add(paper);
    }
    return list;
  }

  /**questionnaire(Enquete is French) */
  static class EnquetePaper {
    /**constructor*/
    public EnquetePaper(String name,ArGender gender,int age,ArSeason season) {
      this.name = name;
      this.gender = gender;
      this.age = age;
      this.season = season;
    }
    /**Full name*/
    private String name;
    /**sex. */
    private ArGender gender;
    /**age*/
    private int age;
    /**Season*/
    private ArSeason season;
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public ArGender getGender() {
      return gender;
    }
    public void setGender(ArGender gender) {
      this.gender = gender;
    }
    public int getAge() {
      return age;
    }
    public void setAge(int age) {
      this.age = age;
    }
    public ArSeason getSeason() {
      return season;
    }
    public void setSeason(ArSeason season) {
      this.season = season;
    }
  }
}

The result is as follows.


Questionnaire
[age=23, gender=male, name=O, season=autumn]
[age=26, gender=Female, name=S, season=spring]
[age=22, gender=Female, name=U, season=autumn]
[age=29, gender=male, name=B, season=autumn]
[age=20, gender=Female, name=C, season=spring]
[age=27, gender=male, name=D, season=winter]
[age=26, gender=Female, name=P, season=spring]
[age=23, gender=male, name=T, season=winter]
[age=23, gender=Female, name=W, season=summer]
[age=20, gender=male, name=N, season=summer]
[age=29, gender=male, name=E, season=autumn]
[age=20, gender=Female, name=M, season=summer]
[age=22, gender=male, name=A, season=autumn]
[age=26, gender=male, name=H, season=autumn]
[age=26, gender=male, name=I, season=autumn]
[age=20, gender=Female, name=L, season=winter]
[age=29, gender=Female, name=V, season=summer]
[age=27, gender=Female, name=Z, season=summer]
[age=20, gender=Female, name=X, season=autumn]
[age=20, gender=Female, name=J, season=summer]
Aggregate by season
spring-> 3
summer-> 6
autumn-> 8
winter-> 3
Aggregate by season-Another solution
spring-> 3
summer-> 6
autumn-> 8
winter-> 3
Aggregate cases for women
spring-> 3
summer-> 5
autumn-> 2
winter-> 1
Aggregate cases for women-Another solution
spring-> 3
summer-> 5
autumn-> 2
winter-> 1
Aggregate by age group
20-24:spring-> 1
20-24:summer-> 4
20-24:autumn-> 4
20-24:winter-> 2
25-29:spring-> 2
25-29:summer-> 2
25-29:autumn-> 4
25-29:winter-> 1


Recommended Posts

Aggregate the results of the "favorite season" questionnaire, and also aggregate by gender and age
What if the results of sum and inject (: +) are different?