已知一个文本内容如下:
name:xiaoming,age:10岁,gender:male,height:172cm,weight:70kg
age:20岁,height:177cm,name:xiaobai,weight:80kg,gender:male
gender:female,height:176cm,weight:66kg,name:xiaolv,age:21岁
每一行是数据为一个人的信息,但是顺序是不固定的。
1.读取这些数据,每一行的数据封装到一个 Person 对象中,并存入一个 List 集合
2.删除集合中所有的未成年的数据
3.计算所有人的平均年龄、最大年龄、最小年龄
4.将集合中的数据按照年龄进行降序排序,若年龄相同,按照身高降序排序
5.查询集合中所有的年龄在[20, 25]范围内,体重在[60, 80]范围内的数据,按照身高降序排列,截取第4名到第8 名。
package com.chr.instance;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
public class Instance01 {
public static void main(String[] args) {
//准备数据
String datasource="name:xiaoming,age:10岁,gender:male,height:172cm,weight:70kg\n" +
"age:20岁,height:177cm,name:xiaobai,weight:80kg,gender:male\n" +
"gender:female,height:176cm,weight:66kg,name:xiaolv,age:21岁";
List<Person> person = getPerson(datasource);
person.forEach(System.out::println);
//删除集合中所有未成年的数据
person.removeIf(e->e.getAge()<18);
//计算所有人的平均年龄,最大年龄,最小年龄
//1.使用集合遍历,获取平均值,最大值和最小值
int max1=0,min1=Integer.MAX_VALUE,sum=0;
for(Person person1 : person){
max1 = Math.max(max1,person1.getAge());
min1 = Math.min(min1,person1.getAge());
sum += person1.getAge();
}
System.out.println("最大年龄:"+max1+"最小年龄:"+min1+"平均年龄:"+(double)sum/person.size());
//2.调用stream方法,使用集合流式编程
IntSummaryStatistics statistics = person.stream().mapToInt(Person::getAge).summaryStatistics();
double average = statistics.getAverage();
int max = statistics.getMax();
int min = statistics.getMin();
System.out.println("所有人的平均年龄为:"+average);
System.out.println("所有人的最大年龄为:"+max);
System.out.println("所有人的最小年龄为:"+min);
//将集合中的元素按照年龄进行降序排列,若年龄相仿,按照身高降序排列
person.sort((p1,p2)->{
int pp = p2.getAge() - p1.getAge();
if(pp != 0){
//如果年龄相减不为0,则说明年龄不相等,返回年龄降序排序
return pp;
}
//如果年龄相等相减为0,则使用身高数据进行降序排序
return p2.getHeight() - p1.getHeight();
});
person.forEach((System.out::println));
//查询集合中所有的年龄在[20, 25]范围内,体重在[60, 80]范围内的数据,按照身高降序排列,截取第4名到第8名
//调用stream方法,使用集合流式编程
List<Person> collect = person.stream()
.filter(p->p.getAge() >= 20 && p.getAge()<=25)
.filter(p->p.getWeight() >= 60 && p.getWeight() <= 80)
.sorted((p1,p2) -> p2.getHeight() - p1.getHeight())
.limit(8)
.skip(3)
.collect(Collectors.toList());
collect.forEach(e-> System.out.println());
//遍历集合
for (Person coll:collect){
System.out.println(coll);
}
}
//创建一个方法用来读取数据源,并存入List集合
public static List<Person> getPerson(String datasource) {
//实例化一个集合对象,用来存储Person对象
List<Person> list = new ArrayList<>();
//以\n做切割,得到每一行的数据
String[] split1 = datasource.split("\n");
//遍历每一行数据
for(String split:split1){
//每一行数据以逗号进行分割,得到每一个对应的属性
String[] split2 = split.split(",");
//实例化一个Person对象
Person person = new Person();
for (String split3:split2){
//遍历所有的属性,并解析所有的值
String[] sp = split3.split(":");
//通过switch匹配每一项的值
switch (sp[0]){
case "name":person.setName(sp[1]);
break;
case "age": person.setAge(Integer.parseInt(sp[1].substring(0,sp[1].length()-1)));
break;
case "gender": person.setGender(Gender.getGender(sp[1]));
break;
case "height": person.setHeight(Integer.parseInt(sp[1].replace("cm","")));
break;
case "weight": person.setWeight(Integer.parseInt(sp[1].replace("kg","")));
break;
}
}
//将Person对象存入集合
list.add(person);
}
return list;
}
}
class Person{
private String name;
private int age;
private int height;
private int weight;
private Gender gender;
public Person(){}
public Person(String name, int age, int height, int weight, Gender gender) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
this.gender = gender;
}
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;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
", weight=" + weight +
", gender=" + gender +
'}';
}
}
enum Gender{
male,female;
public static Gender getGender(String desc){
if(desc.equalsIgnoreCase("male")){
return male;
}else if(desc.equalsIgnoreCase("female")){
return female;
}
return null;
}
}