一题目要求:
二具体代码:
package three;
import sun.util.resources.LocaleData;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class test {
public static void main(String[] args) {
String userStrs = "10001:张三:男:1990-01-01#10002:李四:女:1989-01-09" +
"#10003:上五:男:1999-09-09#10004:各:男:1999-01-01" +
"#10005:孙悟窄:男:1900-01-02#10006:张:女:1999-01-01" +
"#10007:刘备:女:1999-01-01#10004:张sa:女:2003-07-01" +
"#10009:气我:男:1900-01-01";
List<People> peoples=new ArrayList<>();
String[] user=userStrs.split("#");
for (String s : user) {
People m=new People();
String[]data=s.split(":");
m.setId(Long.valueOf(data[0]));
m.setName(data[1]);
m.setGender(data[2]);
m.setBirthday(LocalDate.parse(data[3]));
peoples.add(m);
}
System.out.println(peoples);
}
}
package three;
import sun.util.resources.LocaleData;
import java.time.LocalDate;
public class People {
private long id;
private String name;
private String gender;
private LocalDate birthday;
public People() {
}
public People(long id, String name, String gender, LocalDate birthday) {
this.id = id;
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", birthday=" + birthday +
'}';
}
}
三:业务二:
四具体代码:(主类里面加一个方法)
private static void count(List<People> peoples) {
Map<String,Integer> count=new HashMap<>();
for (People people : peoples) {
if(count.containsKey(people.getName())){
count.put(people.getName(), count.get(people.getName())+1);
}else{
count.put(people.getName(), 1);
}
}
count.forEach((p,c)->{
System.out.println(p+"出现 " +c+"次");
});
}
五运行结果: