4

我正在使用下面的代码并根据某些逻辑使用了两个 continue 语句,但声纳列表显示了这个问题Reduce the total number of break and continue statements in this loop to use at most one.

如何解决这个问题?

for (HashMap<String, String> objRequestIdVO : pObjTicketId) {
    List<TicketDetailsDO> objTicketDetailslist = storeManagerDao.getTicketDetailsWithTicketId(objRequestIdVO.get("requestId"));
    if (null == objTicketDetailslist || objTicketDetailslist.isEmpty()) {
        continue;
    }

    Integer iDesiredDsicount = objTicketDetailslist.get(0).getDesiredDiscount();
    String iSubDept = objTicketDetailslist.get(0).getSubdeptTicket().getSubDeptId();
    List<MCouponDO> objMCounponList = storeManagerDao.getMcouponData(iDesiredDsicount, iSubDept);

    if (null == objMCounponList || objMCounponList.isEmpty()) {
        continue;
    }

    String strHeader = objMCounponList.get(0).getHeader();
    objHeaderVO = new HeaderVO();
    objHeaderVO.setHeader(strHeader);
    objHeaderVO.setRequestId(objRequestIdVO.get("requestId"));
    objHeaderVOList.add(objHeaderVO);
}
4

3 回答 3

5

将空检查继续更改为非空检查并继续。只有在非空检查通过时才会执行代码,这与如果为空则继续执行相同。

for (HashMap<String, String> objRequestIdVO : pObjTicketId) {
    List<TicketDetailsDO> objTicketDetailslist = storeManagerDao.getTicketDetailsWithTicketId(objRequestIdVO.get("requestId"));
    if (!(null == objTicketDetailslist || objTicketDetailslist.isEmpty())) {
        Integer iDesiredDsicount = objTicketDetailslist.get(0).getDesiredDiscount();
        String iSubDept = objTicketDetailslist.get(0).getSubdeptTicket().getSubDeptId();
        List<MCouponDO> objMCounponList = storeManagerDao.getMcouponData(iDesiredDsicount, iSubDept);
        if (!(null == objMCounponList || objMCounponList.isEmpty()) {
            String strHeader = objMCounponList.get(0).getHeader();
            objHeaderVO = new HeaderVO();
            objHeaderVO.setHeader(strHeader);
            objHeaderVO.setRequestId(objRequestIdVO.get("requestId"));
            objHeaderVOList.add(objHeaderVO);
        }
    }
}
于 2016-06-24T05:41:39.770 回答
1

您可以使用流替换过滤器的 continue 。

pObjTicketId.stream()
                .map(m-> m.get("requestId"))
                .map(reqId ->
                        Optional.ofNullable(storeManagerDao.getTicketDetailsWithTicketId(reqId))
                        .filter(l->!l.isEmpty())
                        .map(l->l.get(0))
                        .map(ticketDetails->
                                storeManagerDao.getMcouponData(ticketDetails.getDesiredDiscount(),
                                        ticketDetails.getSubdeptTicket().getSubDeptId())
                        )
                        .filter(Objects::nonNull)
                        .filter(l->!l.isEmpty())
                        .map(l->l.get(0))
                        .map(couponDo-> {
                            HeaderVO headerVO = new HeaderVO();
                            headerVO.setHeader(couponDo.getHeader());
                            headerVO.setRequestId(oreqId);
                            return headerVO;
                        })
                )
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(Collectors.toList());
于 2016-06-24T06:20:55.323 回答
0

你有一个比修复声纳警告更大的问题如果调用 - storeManagerDao.getTicketDetailsWithTicketId(objRequestIdVO.get("requestId"))&storeManagerDao.getMcouponData(iDesiredDsicount, iSubDept)是关于进行数据库调用。这是一个很大的性能点,一个应该 -永远不要从循环中进行数据库调用,这比多个 continue & break 语句危险得多

因此,我将首先重组您的 DAO 调用 -在您的主循环之外一次性storeManagerDao.getTicketDetailsWithTicketId运行INsql 查询并生成一个......这将自动摆脱您的第一个。objRequestIdVO.get("requestId")Map<String,List<TicketDetailsDO>>if

Map<String,List<MCouponDO> objMCounponList>接下来,您通过迭代先前的地图重复相同的过程来构造 a ,Map<String,List<TicketDetailsDO>>其中该地图的键类似于 - iDesiredDsicount|iSubDept

这样,您将有两个断开的循环,并且只有两个 DB 调用,并且您的声纳警告会在途中自动解决。

于 2019-05-06T05:17:33.623 回答