1 背景
如下两个枚举类,依次遍历两个枚举类所有的status和desc通过下划线组成字符串放到同一个list里面,并过滤掉长度不等于7的字符串
@Getter
@AllArgsConstructor
public enum EmailStatusEnum {
TO_SEND(0, "邮件待发送"),
SEND_SUC(1, "邮件已发送"),
SEND_ING(2, "邮件发送中"),
SEND_FAIL(3, "邮件发送失败");
private int status;
private String desc;
}
@Getter
@AllArgsConstructor
public enum PhoneStatusEnum {
TO_SEND(0, "短信待发送"),
SEND_SUC(1, "短信已发送"),
SEND_ING(2, "短信发送中"),
SEND_FAIL(3, "短信发送失败");
private int status;
private String desc;
}
2.实现
2.1 直接遍历-贫血模型
public static void main(String[] args) {
List list = Arrays.stream(EmailStatusEnum.values()).map(e -> e.getStatus() + "_" + e.getDesc()).filter(s -> s.length() == 7).collect(Collectors.toList());
list.addAll(Arrays.stream(PhoneStatusEnum.values()).map(e -> e.getStatus() + "_" + e.getDesc()).filter(s -> s.length() == 7).collect(Collectors.toList()));
System.out.println(list);
}
2.2 实现接口-充血模型
2.2.1 提供一个接口
public interface SendService {
/**
* 获取描述
*
* @return id_desc
*/
String getStatusDesc();
static List getSDList(SendService[]... sendServices) {
return Arrays.stream(sendServices).flatMap(Arrays::stream)
.map(SendService::getStatusDesc).filter(s -> s.length() == 7).collect(Collectors.toList());
}
}
2.2.2 实现SendService
@Getter
@AllArgsConstructor
public enum EmailStatusEnum implements SendService {
TO_SEND(0, "邮件待发送"),
SEND_SUC(1, "邮件已发送"),
SEND_ING(2, "邮件发送中"),
SEND_FAIL(3, "邮件发送失败");
private int status;
private String desc;
@Override
public String getStatusDesc() {
return this.status + "_" + this.desc;
}
}
@Getter
@AllArgsConstructor
public enum PhoneStatusEnum implements SendService {
TO_SEND(0, "短信待发送"),
SEND_SUC(1, "短信已发送"),
SEND_ING(2, "短信发送中"),
SEND_FAIL(3, "短信发送失败");
private int status;
private String desc;
@Override
public String getStatusDesc() {
return this.status + "_" + this.desc;
}
}
2.2.3 调用
public static void main(String[] args) {
List list = SendService.getSDList(EmailStatusEnum.values(), PhoneStatusEnum.values());
System.out.println(list);
}