Java判断两个ListMap值是否相等(包括数据长度、存储的值)超实用工具类(亲测)

// 判断并返回结果 true和false

public static boolean isEqualCollection(Collection a, Collection b) {

if (a.size() != b.size()) { // size是最简单的相等条件

return false;

}

Map mapa = getCardinalityMap(a);

System.out.println(mapa);

Map mapb = getCardinalityMap(b);

System.out.println(mapb);

// 转换map后,能去掉重复的,这时候size就是非重复项,也是先决条件

if (mapa.size() != mapb.size()) {

System.out.println(“存储的map数据不一致!”);

return false;

} else {

System.out.println(“转换map后,能去掉重复的,这时候size就是非重复项后,存储的map数据一致!”);

}

Iterator it = mapa.keySet().iterator();

while (it.hasNext()) {

Object obj = it.next();

// 查询同一个obj,首先两边都要有,而且还要校验重复个数,就是map.value

if (getFreq(obj, mapa) != getFreq(obj, mapb)) {

return false;

}

}

return true;

}

/**

  • 以obj为key,可以防止重复,如果重复就value++ 这样实际上记录了元素以及出现的次数

*/

public static Map getCardinalityMap(Collection coll) {

Map count = new HashMap();

for (Iterator it = coll.iterator(); it.hasNext()😉 {

Object obj = it.next();

Integer c = (Integer) count.get(obj);

if (c == null)

count.put(obj, INTEGER_ONE);

else {

count.put(obj, new Integer(c.intValue() + 1));

}

}

return count;

}

private static final int getFreq(Object obj, Map freqMap) {

Integer count = (Integer) freqMap.get(obj);

if (count != null) {

return count.intValue();

}

return 0;

}

// main方法测试类

public static void main(String[] args) {

Map<String, Object> m1 = new HashMap<String, Object>();

m1.put(“a”, “abc”);

m1.put(“b”, “123”);

m1.put(“c”, “123”);

m1.put(“d”, “123”);

Map<String, Object> m2 = new HashMap<String, Object>();

m2.put(“c”, “123”);

m2.put(“a”, “abc”);

m2.put(“b”, “123”);

m2.put(“d”, “123”);

Map<String, Object> m3 = new HashMap<String, Object>();

m3.put(“a”, “abc”);

m3.put(“b”, “123”);

m3.put(“c”, “123”);

m3.put(“d”, “123”);

面试结束复盘查漏补缺

每次面试都是检验自己知识与技术实力的一次机会,面试结束后建议大家及时总结复盘,查漏补缺,然后有针对性地进行学习,既能提高下一场面试的成功概率,还能增加自己的技术知识栈储备,可谓是一举两得。

以下最新总结的阿里P6资深Java必考题范围和答案,包含最全MySQL、Redis、Java并发编程等等面试题和答案,用于参考~

重要的事说三遍,关注+关注+关注!

历经30天,说说我的支付宝4面+美团4面+拼多多四面,侥幸全获Offer

image.png

更多笔记分享

历经30天,说说我的支付宝4面+美团4面+拼多多四面,侥幸全获Offer

重要的事说三遍,关注+关注+关注!*

[外链图片转存中…(img-sWbtfE3J-1714343642052)]

[外链图片转存中…(img-UnyWv3tS-1714343642053)]

更多笔记分享

[外链图片转存中…(img-PQcHBBv9-1714343642053)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 8
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java中的Stream API来实现这个功能。具体实现方法如下: 1. 使用Stream的map方法将集合中的对象转化成属性的集合; 2. 使用Stream的distinct方法去重; 3. 使用Stream的count方法统计去重后的元素个数,如果为1则说明两个集合中的对象某两个属性全部相等,否则不相等。 示例代码如下: ```java public static boolean isPropertyEqual(List<?> list1, List<?> list2, String propertyName1, String propertyName2) { // 将集合中的对象转化成属性的集合 List<Object> propertyList1 = list1.stream().map(obj -> { try { Field field1 = obj.getClass().getDeclaredField(propertyName1); field1.setAccessible(true); Field field2 = obj.getClass().getDeclaredField(propertyName2); field2.setAccessible(true); return Arrays.asList(field1.get(obj), field2.get(obj)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); // 将集合中的对象转化成属性的集合 List<Object> propertyList2 = list2.stream().map(obj -> { try { Field field1 = obj.getClass().getDeclaredField(propertyName1); field1.setAccessible(true); Field field2 = obj.getClass().getDeclaredField(propertyName2); field2.setAccessible(true); return Arrays.asList(field1.get(obj), field2.get(obj)); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } }).collect(Collectors.toList()); // 去重 propertyList1 = propertyList1.stream().distinct().collect(Collectors.toList()); propertyList2 = propertyList2.stream().distinct().collect(Collectors.toList()); // 统计去重后的元素个数,如果为1则说明两个集合中的对象某两个属性全部相等,否则不相等 return propertyList1.size() == 1 && propertyList2.size() == 1 && propertyList1.get(0).equals(propertyList2.get(0)); } ``` 其中,list1和list2分别表示两个集合,propertyName1和propertyName2分别表示要比较的两个属性名。返回为一个布尔,表示两个集合中的对象某两个属性是否全部相等

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值