[英]Why does the Java8 Stream GroupingBy return empty
@Transactional(isolation = Isolation.READ_UNCOMMITTED
, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void insertPhoneAndArea(List<RecordEntity> recordEntityList) {
int tryCount = 0;
OffsetTime startTime = OffsetTime.ofInstant(Instant.now(), ZoneId.systemDefault());
Map<String, List<RecordEntity>> selfGroup = recordEntityList.stream().collect(Collectors.groupingBy(RecordEntity::getSelfNum));
while (0==selfGroup.size()) {
//There has a bug with Jdk Stream to groupBy in One elements ,So have a loop try
selfGroup = recordEntityList.stream().collect(Collectors.groupingBy(RecordEntity::getSelfNum));
tryCount++;
if (tryCount > 20000) {
break;
}
log.info("try to get SelfNum with Stream bug ! count in {} ",tryCount);
}
}
Steps:脚步:
I don't know what the bug is.不知道是什么bug
@Data
public class RecordEntity {
private int id;
/**
* 通话时间
*/
private Date recordTime;
/**
* 持续时长
*/
private Integer duration;
/**
* 通话方式/ 1电话主叫、2电话被叫、3短信主叫、4短信被叫
*/
private Integer recordMode;
/**
* 对方电话号码
*/
private String dialNum;
/**
* 通话地点
*/
private String recordAddress;
/**
* 通话类型/ 1本地、2异地
*/
private Integer recordType;
/**
* 小区编号
*/
private String area;
/**
* 基站号
*/
private String baseStation;
/**
* 交换机号
*/
private String exchange;
/**
* 本机号码
*/
private String selfNum;
/**
* 机主姓名
*/
private String username;
/**
* 机主身份证
*/
private String idNum;
/**
* 通话记录id(唯一)
*/
private String recordId;
/**
* 话单运营商
*/
private Integer operator;
/**
* 对方号码类型(1-手机,2-固话,3-服务号)
*/
private Integer dialNumType;
}
If Map<String, List<RecordEntity>> selfGroup = recordEntityList.stream().collect(Collectors.groupingBy(RecordEntity::getSelfNum));
如果
Map<String, List<RecordEntity>> selfGroup = recordEntityList.stream().collect(Collectors.groupingBy(RecordEntity::getSelfNum));
sets selfGroup
to an empty map, it must be because recordEntityList
is empty.将
selfGroup
设置为空映射,一定是因为recordEntityList
为空。
Why the caller is passing an empty list to your method I obviously cannot tell.为什么调用者将一个空列表传递给您的方法,我显然无法分辨。
PS You don't need the Yoda condition, 0==selfGroup.size()
. PS 你不需要尤达条件,
0==selfGroup.size()
。 I'd recommend selfGroup.isEmpty()
.我推荐
selfGroup.isEmpty()
。
any chance that selfNum is null? selfNum 是否有可能为空? you can't group by null, actually, you'll get an exception.
你不能按 null 分组,实际上,你会得到一个例外。 if not, try using phrase in another locale.
如果没有,请尝试在其他语言环境中使用短语。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.