Get average age of male
members
import java.time.LocalDate;
import java.time.chrono.IsoChronology;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;
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 of male members (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.com"));
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);
}
}
Check if there is any
students from Milan
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public
class Main
{
public
static
void main(String...args)
{
Student raoul =
new Student(
"Raoul",
"Cambridge");
Student mario =
new Student(
"Mario",
"Milan");
Student alan =
new Student(
"Alan",
"Cambridge");
Student brian =
new Student(
"Brian",
"Cambridge");
List transactions = Arrays.asList(
new Graduate(brian, 2011, 300),
new Graduate(raoul, 2012, 1000),
new Graduate(raoul, 2011, 400),
new Graduate(mario, 2012, 710),
new Graduate(mario, 2012, 700),
new Graduate(alan, 2012, 950)
);
boolean milanBased =
transactions.stream()
.anyMatch(transaction -> transaction.getTrader()
.getCity()
.equals(
"Milan")
);
System.out.println(milanBased);
}
}
class Student
{
private String name;
private String city;
public Student(String n, String c)
{
this.name = n;
this.city = c;
}
public String getName()
{
return this.name;
}
public String getCity()
{
return this.city;
}
public
void setCity(String newCity)
{
this.city = newCity;
}
public String toString()
{
return
"Student:"+this.name +
" in " + this.city;
}
}
class Graduate
{
private Student trader;
private
int year;
private
int value;
public Graduate(Student trader,
int year,
int value)
{
this.trader = trader;
this.year = year;
this.value = value;
}
public Student getTrader()
{
return this.trader;
}
public
int getYear()
{
return this.year;
}
public
int getValue()
{
return this.value;
}
public String toString()
{
return
"{" + this.trader +
", " +
"year: "+this.year+
", " +
"value:" + this.value +
"}";
}
}
No comments:
Post a Comment