Java测试List<Object>根据其某个属性去重俩种方法效率

需要用到根据gpsTime double类型对List进行去重;
尝试了俩种办法,就像知道耗时与性能;

1. 法一:


// 根据gpsTime去重
imagePostList = imagePostList.stream().collect(
     collectingAndThen(
             toCollection(() -> new TreeSet<>(comparing(n -> n.getGpsTime()))), ArrayList::new));

2. 法二:

// List遍历的同时删除元素需要用Iterator迭代元素,否则会报错
Set<Double> gpsTimeSet = new HashSet<>();
// 删除重复的元素
Iterator<ImagePost> iterator = imagePostList.iterator();
while (iterator.hasNext()) {
    ImagePost imagePost = iterator.next();
    if (gpsTimeSet.contains(imagePost.getGpsTime())) {
        log.error("duplicate: id: {}, name: {}, gpsTime: {}", imagePost.getId(), imagePost.getName(), imagePost.getGpsTime());
        iterator.remove();
    } else {
        gpsTimeSet.add(imagePost.getGpsTime());
    }
}

3. 结果

Java测试List<Object>根据其某个属性去重俩种方法效率_第1张图片少量数据发现是 Iterator遍历删除效率更高

那么数据量很大的情况下呢?
Java测试List<Object>根据其某个属性去重俩种方法效率_第2张图片
很明显,数据量大的时候,还是用Iterator迭代的同时删除重复元素效率更高一些;
多次运行,效果一致,仍然是Iterator更高效

4. 源代码

import java.util.*;

import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/*************************************
 * Class Name: TestRemoveDuplicateGpsTime
 * Description:〈测试去重gpsTime效率〉
 * @create 2020/10/16
 * @since 1.0.0
 ************************************/
public class TestRemoveDuplicateGpsTime {

    public static void main(String[] args) {
        TestRemoveDuplicateGpsTime test = new TestRemoveDuplicateGpsTime();
        test.testEfficency();
    }

    public void testEfficency() {
        List<ImagePost> imagePostList = new ArrayList<>();
        initImagePost(imagePostList);
        long time1 = System.currentTimeMillis();
        // 根据gpsTime去重
        imagePostList = imagePostList.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(n -> n.getGpsTime()))), ArrayList::new));
        long time2 = System.currentTimeMillis();
        System.out.println("当前程序耗时:" + (time2 - time1) + "ms");
        System.out.println("after GpsTime去重size: " + imagePostList.size());

        System.out.println("-----------------------------------------------");
        imagePostList.clear();
        initImagePost(imagePostList);
        time1 = System.currentTimeMillis();
        removeDuplicate(imagePostList);
        time2 = System.currentTimeMillis();
        System.out.println("当前程序耗时:" + (time2 - time1) + "ms");
    }

    public void removeDuplicate(List<ImagePost> imagePostList) {
        Set<Double> gpsTimeSet = new HashSet<>();
        // 删除重复的元素
        Iterator<ImagePost> iterator = imagePostList.iterator();
        while (iterator.hasNext()) {
            ImagePost imagePost = iterator.next();
            if (gpsTimeSet.contains(imagePost.getGpsTime())) {
                System.out.println("duplicate: id: " + imagePost.getId() + ",gpsTime: " + imagePost.getGpsTime());
                iterator.remove();
            } else {
                gpsTimeSet.add(imagePost.getGpsTime());
            }
        }
        System.out.println("after removeDuplicate, count: " + imagePostList.size());
    }

    public void initImagePost(List<ImagePost> imagePostList) {
        imagePostList.add(new ImagePost(1, 11472.15475));
        imagePostList.add(new ImagePost(2, 11476.1));
        imagePostList.add(new ImagePost(3, 11475.2));
        imagePostList.add(new ImagePost(4, 11332.1));
        imagePostList.add(new ImagePost(5, 11433.5));
        imagePostList.add(new ImagePost(6, 11472.15475));
        imagePostList.add(new ImagePost(7, 11476.1));
        imagePostList.add(new ImagePost(8, 11987.32));
    }

    class ImagePost {
        Integer id;
        double gpsTime;

        public ImagePost(Integer id, double gpsTime) {
            this.id = id;
            this.gpsTime = gpsTime;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public double getGpsTime() {
            return gpsTime;
        }

        public void setGpsTime(double gpsTime) {
            this.gpsTime = gpsTime;
        }
    }
}

参考:

-https://www.cnblogs.com/qq1141100952com/p/11197120.html

你可能感兴趣的:(JAVA,大数据,java,删除重复数据效率,removeDuplicate)