简体   繁体   English

为什么 Java8 Stream GroupingBy 返回空

[英]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:脚步:

  1. Invoking the method with name insertPhoneAndArea调用名为 insertPhoneAndArea 的方法
  2. Given the same data with variables, with "recordEntityList" in there, and it was not empty给定带有变量的相同数据,其中包含“recordEntityList”,并且它不为空
  3. When I executed the method, the selfGroup was empty and it's size was zero.当我执行该方法时,selfGroup 是空的,它的大小为零。 So I feel strange with the result.所以我对结果感到奇怪。 It shouldn't be empty because the recordEntityList was not empty.它不应该为空,因为 recordEntityList 不为空。
  4. I turned on the Debug Mode with IDEA and I was using Alt+LeftClick to execute the code and the result was correct, it wasn't empty.我用IDEA打开调试模式,我用Alt+LeftClick来执行代码,结果是正确的,不是空的。
  5. So I wrote a try count in the group code, as expected, that code in the loop was approximately random up to 30, and it was a correct result and it was out of the while loop.所以我在组代码中写了一个尝试计数,正如预期的那样,循环中的代码大约随机到30,并且它是正确的结果并且它在while循环之外。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM