集合中对象去重

2019-11-26  本文已影响0人  JiangCheng97

集合中对象去重

  1. HashMap
        Kill kill1 = new Kill();
        Kill kill2 = new Kill();
        Map<String, Object> mapList = new HashMap<>();
        List<Kill> killList = new ArrayList<>();
        killList.add(kill1);
        killList.add(kill2);
        for (Kill kill : killList) {
            if (!mapList.containsKey("" + kill.toString().hashCode())) {
                mapList.put(kill.toString().hashCode() + "", kill);
            }
        }
        for (Object kill : mapList.values()){
            System.out.println(kill);
        }
上一篇下一篇

猜你喜欢

热点阅读