题目
String userData = "10000:张三:男:1998-01-01#10001:张三:男:1998-01-01#10002:李四:女:1999-02-02#10003:王五:男:2000-03-03#10004:赵六:女:2001-04-04";
String[] usersArray = userData.split("#");
// 使用Stream API将字符串数组转换为SysUser对象数组
List<SysUser> users = new ArrayList<>();
Arrays.stream(usersArray).forEach(user -> {
String[] userArray = user.split(":");
// date format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
SysUser sysUser = new SysUser(userArray[0], userArray[1], userArray[2], formatter.parse(userArray[3], LocalDate::from));
users.add(sysUser);
});
// 打印输出
users.forEach(System.out::println);
// 获取每个名字各自的数量 使用Map封装
Map<String, Long> idCountMap = users.stream().collect(Collectors.groupingBy(SysUser::getName, Collectors.counting()));
System.out.println(idCountMap);
User类
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class SysUser {
private String id;
private String name;
private String gender;
private LocalDate birthday;
}