Objects.equals小坑

本文分析了Java中Integer.equals方法的行为,指出当尝试用Integer对象与long类型进行equals比较时,由于类型不匹配,会导致返回false。问题源于Integer.equals方法首先检查参数是否为Integer类型。解决方法是将比较的双方转换为相同的类型。这个情况同样适用于其他基本类型包装类的equals方法。理解这一点对于避免潜在的bug至关重要。

问题描述

Integer a = 1;
long b = 1L;
System.out.println(Objects.equals(b, a));
//结果:false

原因分析:

Integer的equals方法:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
先判断参数obj是否是Integer类型,如果不是,则直接返回false。如果是Integer类型,再进一步判断int值是否相等。

而上面这个例子中b是long类型,所以Integer的equals方法直接返回了false。

也就是说,如果调用了Integer的equals方法,必须要求入参也是Integer类型,否则该方法会直接返回false。

解决方案:

将参数b的类型强制转换成int,或者将参数a的类型强制转换成long。
Integer a = 1;
long b = 1L;
System.out.println(Objects.equals(a, (int)b));
//结果:true

Integer a = 1;
long b = 1L;
System.out.println(Objects.equals(b, (long)a));
//结果:true

java的8种基本类型包装类的equals都有相同的问题

public static void main(String[] args) { //3、取当前页的插坑商品goodId + int goodsPitSize = 1; + String goodsIds="123"; + List<String> goodsIdList = Arrays.stream(goodsIds.split(",")).map(String::trim).toList(); + int pageNum=2; + int pageSize=20; + List<String> insertGoodsIdList = goodsIdList.subList((pageNum - 1) * goodsPitSize, Math.min(pageNum * goodsPitSize, goodsIdList.size())); + } @Override public List<AwardListDTO> process(AwardListContext context, List<AwardListDTO> output) { try { + AwardListRequestDTO requestDTO = context.getRequestDTO(); + EasyWinGoodsInsertConfig goodsInsertPitConfig = context.getPlayContext().getActMainEntireConfig().getGoodsInsertConfig(); + if (Objects.isNull(goodsInsertPitConfig) || !Objects.equals(goodsInsertPitConfig.getIsOpen(), 1)) { + return output; } // 仅全部tab + if (StringUtils.isNotBlank(context.getRequestDTO().getCategory())) { + return output; } //2、后台的商品ID进行分组,取当前页待插坑的商品 + List<Integer> goodsPitList = Arrays.stream(goodsInsertPitConfig.getGoodsPits().split(",")).map(Integer::valueOf).toList(); + List<String> goodsIdList = Arrays.stream(goodsInsertPitConfig.getGoodsIds().split(",")).map(String::trim).toList(); //3、取当前页的插坑商品goodId + int goodsPitSize = goodsPitList.size(); + int fromIndex = (requestDTO.getPageNum() - 1) * goodsPitSize; + int toIndex = Math.min(requestDTO.getPageNum() * goodsPitSize, goodsIdList.size()); + if (fromIndex >= goodsIdList.size()) { + return output; } + List<String> insertGoodsIdList = goodsIdList.subList(fromIndex, toIndex); //如果推荐返回的商品数小于坑位数,则不做插坑 + if (CollectionUtils.isEmpty(insertGoodsIdList) || output.size() < insertGoodsIdList.size()) { + return output; } //4、goodsId重复,过滤投放品,并插入插坑品 + output.removeIf(e -> insertGoodsIdList.contains(e.getGoodsId())); //5、调投放获取待插坑的商品信息 + LubanFeedListRequest lubanFeedListRequest = buildManualLuBanFeedListRequest(easyWinInsertGoodsFeedDid, context, insertGoodsIdList); + LubanDeliveryFeedDTO lubanDeliveryFeedDTO = feedsManager.getFeedsGoodsList(lubanFeedListRequest, requestDTO.getSite()); + if (CollectionUtils.isEmpty(lubanDeliveryFeedDTO.getGoods())) { + log.warn("GoodsInsertHandler response is null"); + return output; } + List<AwardListDTO> insertGoodsInfoList = new ArrayList<>(); + for (LubanDeliveryFeedDTO.Goods goods : lubanDeliveryFeedDTO.getGoods()) { + if (Objects.isNull(goods.getRetailPrice()) || Objects.isNull(goods.getSalePrice()) + || Objects.isNull(goods.getRetailPrice().getAmount()) || Objects.isNull(goods.getRetailPrice().getUsdAmount()) + || Objects.isNull(goods.getSalePrice().getUsdAmount())) { + continue; } //插坑品校验 + if (new BigDecimal(goods.getSalePrice().getUsdAmount()).compareTo(new BigDecimal(goodsInsertPitConfig.getUpperLimit())) > 0) { + continue; } + AwardListDTO insertGoodsDTO = constructAwardListGoods(context.getRequestDTO(), goods); + insertGoodsInfoList.add(insertGoodsDTO); + } //6、插坑 + for (int i = 0; i < insertGoodsInfoList.size(); i++) { + AwardListDTO newElement = insertGoodsInfoList.get(i); + int position = goodsPitList.get(i) - 1; // 插入元素,超出推荐列表的坑位依次追加在后面 + if (position >= output.size()) { + output.add(newElement); + } else if (Objects.equals(output.get(position).getAwardType(), MAIN_GOODS.getKey())) { + output.add(position, newElement); } } + return output; + } catch (Exception e) { + log.error("GoodsInsertHandler err", e); + return output; } } + int goodsPitSize = 1; + String goodsIds="123"; + List<String> goodsIdList = Arrays.stream(goodsIds.split(",")).map(String::trim).toList(); + int pageNum=2; + int pageSize=20; + List<String> insertGoodsIdList = goodsIdList.subList((pageNum - 1) * goodsPitSize, Math.min(pageNum * goodsPitSize, goodsIdList.size())); + }这里是啥意思,写死了个123
09-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值