Java测试List<Object>根据其某个属性去重俩种方法效率
生活随笔
收集整理的這篇文章主要介紹了
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. 結果
少量數據發現是 Iterator遍歷刪除效率更高
那么數據量很大的情況下呢?
很明顯,數據量大的時候,還是用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测试List<Object>根据其某个属性去重俩种方法效率的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python line_profile
- 下一篇: MySQL语句第二高的薪水查询