import java.util.*;
public class testMain {
public static class Person {
int age;
String name;
Person (int age,String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static void main(String[] args) {
LinkedHashSet<Person> pList = new LinkedHashSet<>(
Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"),
new Person(33,"john"), new Person(44,"mickey"),
new Person(55,"fiona"),new Person(55,"fiona")));
LinkedHashSet<Person> pList1 = new LinkedHashSet<>(
Arrays.asList(new Person(11,"mike"),new Person(22,"lynn"),
new Person(33,"john"), new Person(44,"mickey")));
System.out.println("对象集合的总年龄:");
System.out.println(pList.stream().map(Person::getAge).reduce(0,Integer::sum));
System.out.println("对象集合的平均年龄:");
System.out.println(pList.stream().map(Person::getAge).reduce(0, Integer::sum) / pList.size());
}
}
对象集合的总年龄:
220
对象集合的平均年龄:
36
开发工具:IntelliJ IDEA 2022.2.1 (Ultimate Edition)
参考文献:https://blog.csdn.net/lijingronghcit/article/details/108348728