import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;
public class StreamController {
static class User {
public User(String userName, String score, String userCode, String phone) {
this.userName = userName;
this.score = score;
this.userCode = userCode;
this.phone = phone;
}
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
private String score;
private String userCode;
private String phone;
}
public static void main(String[] args) {
List<User> userList = new ArrayList();
userList.add(new User("李四", "10,20,30", "lisi", "100001"));
userList.add(new User("王五", "40,90,80", "wangwu", "100002"));
userList.add(new User("张三", "50,60,70", "zhangsan", "100003"));
List<User> scorePoList = userList.stream().map(user -> {
String unitNameIds = user.getScore();
if (StringUtils.isNotBlank(unitNameIds) && unitNameIds.contains(",")) {
return Arrays.stream(unitNameIds.split(",")).map(un -> {
User newUser = new User(user.getUserName(), un, user.getUserCode(), user.getPhone());
return newUser;
}).collect(Collectors.toList());
} else {
return Arrays.asList(user);
}
}).flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(JSONObject.toJSONString(scorePoList));
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88