java判断list为空、xxx==null和xxx.equals(null)的区别

本文介绍了在Java中如何正确地使用isEmpty()方法来判断集合是否为空,并对比了与size()==0的区别。此外,还讨论了null检查的重要性及推荐的字符串判断方式。

转载请注明出处:http://blog.youkuaiyun.com/mr_leixiansheng/article/details/75003989

if(null == list || list.size() ==0)

list.isEmpty()和list.size()==0 没有区别

isEmpty()判断有没有元素
而size()返回有几个元素
如果判断一个集合有无元素 
建议用isEmpty()方法.这清晰,简明

list!=null跟!list.isEmpty()有什么区别?

这就相当与,你要喝水,
前面就是判断是不是连水杯都没有,
后面就是判断水杯里面没有水,
连盛水的东西都没有,
这个水从何而来?
所以一般的判断是
if(list!=null && !list.isEmpty()){
这个里面取list中的值
}else{
做其他处理
}

如果xxx不是null的话,xxx==null将返回false,如果xxx是null的话,xxx将返回ture

而对xxx.equals(null)而言,他将永远返回false。如果xxx是null的话,将会抛出空指针异常NullPointerException

总结一句话就是:永远不要用xxx.equals(null),因为没有意义。

推荐 str != null && !str.equals("") //判断字符串不为空

实体类中package com.ruoyi.system.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import java.util.Date; import lombok.Data; /** * 员工排班记录表 * @TableName schedule_record */ @TableName(value ="schedule_record") @Data public class ScheduleRecord { /** * */ @TableId(type = IdType.AUTO) private Long id; /** * 员工ID */ private Long userId; @TableField(exist = false) private String userName; /** * 部门ID */ private Long deptId; @TableField(exist = false) private String deptName; /** * 排班日期 */ private Date scheduleDate; /** * 班次类型(morning/afternoon/night/off) */ private String shiftType; /** * 备注 */ private String remark; /** * */ private String createBy; /** * */ private Date createTime; /** * */ private String updateBy; /** * */ private Date updateTime; @TableField(exist = false) private Integer pageNum; @TableField(exist = false) private Integer pageSize; @Override public boolean equals(Object that) { if (this == that) { return true; } if (that == null) { return false; } if (getClass() != that.getClass()) { return false; } ScheduleRecord other = (ScheduleRecord) that; return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId())) && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId())) && (this.getDeptId() == null ? other.getDeptId() == null : this.getDeptId().equals(other.getDeptId())) && (this.getScheduleDate() == null ? other.getScheduleDate() == null : this.getScheduleDate().equals(other.getScheduleDate())) && (this.getShiftType() == null ? other.getShiftType() == null : this.getShiftType().equals(other.getShiftType())) && (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark())) && (this.getCreateBy() == null ? other.getCreateBy() == null : this.getCreateBy().equals(other.getCreateBy())) && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime())) && (this.getUpdateBy() == null ? other.getUpdateBy() == null : this.getUpdateBy().equals(other.getUpdateBy())) && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime())); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((getId() == null) ? 0 : getId().hashCode()); result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode()); result = prime * result + ((getDeptId() == null) ? 0 : getDeptId().hashCode()); result = prime * result + ((getScheduleDate() == null) ? 0 : getScheduleDate().hashCode()); result = prime * result + ((getShiftType() == null) ? 0 : getShiftType().hashCode()); result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode()); result = prime * result + ((getCreateBy() == null) ? 0 : getCreateBy().hashCode()); result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode()); result = prime * result + ((getUpdateBy() == null) ? 0 : getUpdateBy().hashCode()); result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode()); return result; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(getClass().getSimpleName()); sb.append(" ["); sb.append("Hash = ").append(hashCode()); sb.append(", id=").append(id); sb.append(", userId=").append(userId); sb.append(", deptId=").append(deptId); sb.append(", scheduleDate=").append(scheduleDate); sb.append(", shiftType=").append(shiftType); sb.append(", remark=").append(remark); sb.append(", createBy=").append(createBy); sb.append(", createTime=").append(createTime); sb.append(", updateBy=").append(updateBy); sb.append(", updateTime=").append(updateTime); sb.append("]"); return sb.toString(); } }
09-28
public static List<Map<String, String>> getInventoryStatisticsListMap(List<EscrowLedgerVO> list, String inventoryCategory) { List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); int no = 0; for (EscrowLedgerVO ledgerVO : list) { no += 1; Map<String, String> lm = new HashMap<String, String>(); lm.put("no", String.valueOf(no)); lm.put("parentCategoryName", ledgerVO.getParentCategoryName()); lm.put("categoryName", ledgerVO.getCategoryName()); lm.put("childCategoryName", ledgerVO.getChildCategoryName()); lm.put("name", ledgerVO.getStandardAsset() != null ? ledgerVO.getStandardAsset().getName() : ""); lm.put("model", ledgerVO.getStandardAsset() != null ? ledgerVO.getStandardAsset().getModel() : ""); lm.put("manufacturer", ledgerVO.getStandardAsset().getManufacturer() != null ? ledgerVO.getStandardAsset().getManufacturer().getName() : ""); lm.put("unit", ledgerVO.getStandardAsset() != null ? ledgerVO.getStandardAsset().getUnit() : ""); lm.put("userDept", ledgerVO.getUseDept()); lm.put("managementDept", ledgerVO.getManagementDept() != null ? ledgerVO.getManagementDept().getName() : ""); lm.put("user", ledgerVO.getUsePerson()); lm.put("position", ledgerVO.getPosition() != null ? ledgerVO.getPosition().getName() : ""); lm.put("originalValue", ledgerVO.getOriginalValue().stripTrailingZeros().toPlainString()); lm.put("num", String.valueOf(ledgerVO.getNum())); String firstInventoryNum = ""; String secondInventoryNum = ""; String inventoryWinAndLossNum = ""; if (ledgerVO.getFirstInventoryType().equals("0")) { firstInventoryNum = ""; inventoryWinAndLossNum = ""; } else if (ledgerVO.getFirstInventoryType().equals("1")) { firstInventoryNum = String.valueOf(ledgerVO.getFirstInventoryNum()); inventoryWinAndLossNum = String.valueOf(ledgerVO.getFirstInventoryWinAndLossNum()); } else if (ledgerVO.getFirstInventoryType().equals("2")) { firstInventoryNum = String.valueOf(ledgerVO.getFirstInventoryNum()); if (ledgerVO.getSecondInventoryWinAndLossNum() < 0) { inventoryWinAndLossNum = String.valueOf(ledgerVO.getSecondInventoryWinAndLossNum()); } else { inventoryWinAndLossNum = "-" + ledgerVO.getSecondInventoryWinAndLossNum(); } } else if (ledgerVO.getFirstInventoryType().equals("3")) { firstInventoryNum = String.valueOf(ledgerVO.getFirstInventoryNum()); inventoryWinAndLossNum = "+" + ledgerVO.getFirstInventoryWinAndLossNum(); } //当导出复盘统计数据时,添加复盘数量 if (inventoryCategory ==null || inventoryCategory.equals("1")) { if (ledgerVO.getSecondInventoryType().equals("0") || (ledgerVO.getSecondInventoryNum() == 0 && ledgerVO.getSecondInventoryWinAndLossNum() == 0)) { secondInventoryNum = ""; } else if (ledgerVO.getSecondInventoryType().equals("1")) { secondInventoryNum = String.valueOf(ledgerVO.getSecondInventoryNum()); inventoryWinAndLossNum = String.valueOf(ledgerVO.getSecondInventoryWinAndLossNum()); } else if (ledgerVO.getSecondInventoryType().equals("2")) { secondInventoryNum = String.valueOf(ledgerVO.getSecondInventoryNum()); if (ledgerVO.getSecondInventoryWinAndLossNum() < 0) { inventoryWinAndLossNum = String.valueOf(ledgerVO.getSecondInventoryWinAndLossNum()); } else { inventoryWinAndLossNum = "-" + ledgerVO.getSecondInventoryWinAndLossNum(); } } else if (ledgerVO.getSecondInventoryType().equals("3")) { secondInventoryNum = String.valueOf(ledgerVO.getSecondInventoryNum()); inventoryWinAndLossNum = "+" + ledgerVO.getSecondInventoryWinAndLossNum(); } } lm.put("firstInventoryNum", firstInventoryNum); lm.put("firstInventoryWinAndLossNum", ledgerVO.getFirstInventoryWinAndLossNum().toString()); lm.put("secondNum", ledgerVO.getSecondInventoryNum() == null ? "" : String.valueOf(ledgerVO.getNum())); lm.put("inventoryWinAndLossNum", inventoryWinAndLossNum); lm.put("secondInventoryNum", ledgerVO.getSecondInventoryNum() == null ? "" : secondInventoryNum); lm.put("secondInventoryWinAndLossNum", ledgerVO.getSecondInventoryNum() == null ? "" : ledgerVO.getSecondInventoryWinAndLossNum().toString()); lm.put("remarks", ledgerVO.getRemarks()); listMap.add(lm); } return listMap; } 这是我导出数据的代码,现在需要添加汇总的代码,该如何做
09-18
public List<AtmlCompanyBase> getAtmlCompanyList(AtmlBaseQueryVO queryVO){ List<AtmlCompanyBase> results = new ArrayList<>(); List<AtmlCompanyBase> atmls = null; if ("072".equals(queryVO.getProcType()) || StringUtils.isBlank(queryVO.getProcType())) { atmls = atmlCompanyBaseRepository.findAtmlCompanyBase(); } else if ("081".equals(queryVO.getProcType())) { atmls = atmlCompanyBaseRepository.findAtmlCompanyBaseReEvaluation(); } if (atmls != null && atmls.size() > 0) { for (AtmlCompanyBase at : atmls) { boolean addFlag = true; if (StringUtils.isNotBlank(queryVO.getCompanyName()) && (StringUtils.isBlank(at.getPartyAccName()) || !at.getPartyAccName().contains(queryVO.getCompanyName())) && (StringUtils.isBlank(at.getCompanyName()) || !at.getCompanyName().contains(queryVO.getCompanyName()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getRiskLevelPre()) && (StringUtils.isBlank(at.getRiskLevelPre()) || !at.getRiskLevelPre().equals(queryVO.getRiskLevelPre()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getRiskLevelRe()) && (StringUtils.isBlank(at.getRiskLevelRe()) || !at.getRiskLevelRe().equals(queryVO.getRiskLevelRe()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getStatusStr()) && (StringUtils.isBlank(at.getStatusStr()) || !at.getStatusStr().equals(queryVO.getStatusStr()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getBusinessScene()) && (StringUtils.isBlank(at.getBusinessScene()) || !at.getBusinessScene().equals(queryVO.getBusinessScene()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getFinishTs()) && (at.getFinishTs() == null || !SystemUtil.date2Str(at.getFinishTs(), "yyyyMMdd").equals(queryVO.getFinishTs()))) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getCounterpartyType()) && !queryVO.getCounterpartyType().equals(at.getCounterpartyType())) { addFlag = false; } if (StringUtils.isNotBlank(queryVO.getCancel()) && !queryVO.getCancel().equals(at.getCancel())) { addFlag = false; } if (addFlag) { results.add(at); } } } return results; },实体类中新增了一个字段,但是最终结果没有该字段数据,怎么回事
最新发布
11-11
if (!"母公司汇总".equals(department)) { for (DebtBusinessDayReportWholeAssetVO vo : wholeVO) { if (!department.equals(vo.getDept())) continue; if (!vo.getBusiBigcName().contains("合计")) continue; totalYear = totalYear.add(Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO)); totalLastYear = totalLastYear.add(Optional.ofNullable(vo.getCompareLastYearWorkDay()).orElse(BigDecimal.ZERO)); String busiName = vo.getBusiBigcName(); if (busiName != null && bizTypes.contains(busiName)) { BigDecimal palYear = Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO); bizMap.put(busiName, bizMap.get(busiName).add(palYear)); } } } else { for(DebtBusinessDayReportWholeAssetVO vo : wholeVO){ String projType = vo.getProjType(); // 只累加"合计"值 if (projType == null || !projType.contains("合计")){ continue; } if (!vo.getDept().contains("合计")) continue; totalYear = Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO); totalLastYear = totalLastYear.add(Optional.ofNullable(vo.getCompareLastYearWorkDay()).orElse(BigDecimal.ZERO)); if (vo.getDept().equals("证券投资部")){ totalBus1 = totalBus1.add(Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO)); } if (vo.getDept().equals("金融创新部")){ totalBus2 = totalBus2.add(Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO)); } if (vo.getDept().equals("固定收益部")){ totalBus3 = totalBus3.add(Optional.ofNullable(vo.getPalYear()).orElse(BigDecimal.ZERO)); } } }改成这样的逻辑是什么
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值