Table of Contents ⇒ Java Algorithm Library-Artery-Sample
Q01_10.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.ArMatch;
import jp.avaj.lib.def.ArRepeat;
import jp.avaj.lib.test.L;
/**
* List<Person>Aggregate the number of people every 10 years from
*
*-Use the following methods of ArList.
* Integer[] histogram(Collection<T0> collection,ArCreator<T0,T1> creator,ArValidator<T1>[] validators)
*
*-Use creator to extract data to be aggregated from object T0 of List.
*・ In this sample, the age is extracted from the Person object..
*
*· Use validators to stratify extracted ages.
*・ Although it is an array, it should return true every 10 years..
*
*/
public class Q01_10 {
public static void main(String[] args) throws Exception {
List<Person> personList = createPersonList();
//
//ArCreator ⇒ Extract age from Person
ArCreator<Person,Integer> creator = new ArCreator<Person,Integer>() {
@Override
public Integer convert(Person obj) throws Exception {
return obj.getAge();
}
};
//ArValidator for determining age
ArValidatorIntRange[] validators = new ArValidatorIntRange[] {
new ArValidatorIntRange(10,20,ArMatch.YES), // 10(Including)~20(Not included),ArMatch.YES,NO reverses the judgment
new ArValidatorIntRange(20,30,ArMatch.YES),
new ArValidatorIntRange(30,40,ArMatch.YES),
new ArValidatorIntRange(40,50,ArMatch.YES),
new ArValidatorIntRange(50,60,ArMatch.YES),
};
Integer[] result;
//Create a histogram
result = ArList.histogram(personList,creator, validators);
L.p(ArObj.toString2Arrays(validators,result));
L.p("Another solution");
//Another solution of ArCreator.Use ArCreatorByName to get the value from the field name as a generic ArCreator.
creator = new ArCreatorByName<Person,Integer>("age");
//Another solution of ArValidator
//Creating ArValidator one by one like the first solution is tedious.
//It can also be easily generated as follows.Note, null can be specified at both ends ⇒ infinite range
validators = ArValidatorUtil.createArValidatorIntRangeArray(new Integer[]{10,20,30,40,50,60});
result = ArList.histogram(personList,creator, validators);
L.p(ArObj.toString2Arrays(validators,result));
}
private static List<Person> createPersonList() {
//Class for generating a name ⇒ Name is a single uppercase letter
//ArRandomFromArray fetches the value randomly from the array, ArRepeat specifies whether to fetch the same value.
ArRandomFromArray<String> names = new ArRandomFromArray<String>(ArCharDef.upperStr,ArRepeat.NO);
//Class for generating age ⇒ 10(Including)~60(Not included)Randomly generate an Integer
ArRandomFromInteger ages = new ArRandomFromInteger(10,60);
List<Person> list = new ArrayList<Person>();
for (int i=0; i<20; i++) {
Person person = new Person(names.get(),ages.get());
L.p(ArObj.toString(person)); //By doing this, the contents will be displayed even if toString is not implemented..
list.add(person);
}
return list;
}
static class Person {
public Person(String name,int age) {
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}
The result is as follows
result.txt
[age=31, name=S]
[age=26, name=F]
[age=42, name=K]
[age=43, name=Y]
[age=22, name=M]
[age=34, name=N]
[age=11, name=E]
[age=36, name=Q]
[age=13, name=U]
[age=41, name=X]
[age=39, name=Z]
[age=47, name=O]
[age=47, name=P]
[age=59, name=H]
[age=52, name=A]
[age=17, name=D]
[age=35, name=C]
[age=47, name=W]
[age=55, name=J]
[age=11, name=R]
[10-20) -> 4
[20-30) -> 2
[30-40) -> 5
[40-50) -> 6
[50-60) -> 3
Another solution
[10-20) -> 4
[20-30) -> 2
[30-40) -> 5
[40-50) -> 6
[50-60) -> 3
Recommended Posts