去除"数组"中重复的数据

去重:去除数据中重复的数据

public class Map_offRepetition {
    public static void main(String[] args) {
        Integer[] nums = new Integer[]{1, 1, 1, 1, 2, 1, 2, 3, 3, 3, 5, 5, 4, 5, 4};
        Repetition repetition = new Repetition();
        repetition.offRepetition(nums);

    }



上面是测试:
//--------------
下面是对"去重"封装的方法:


    
   static class Repetition {
        public void offRepetition(Integer[] nums) {
            List list = new ArrayList<>();
            Map<Integer, Object> objectObjectMap = new HashMap<>();
            for (Integer number : nums) {
                objectObjectMap.put(number, true);
            }
            Set set = objectObjectMap.keySet();
            Iterator iterator = set.iterator();
            while ((iterator.hasNext())) {
                list.add(iterator.next());
            }
            System.out.println(list);
        }
    }
}

posted on 2022-07-21 14:18  陈嘻嘻-  阅读(41)  评论(0编辑  收藏  举报

导航