Top-N 泛型工具类

一、代码实现

public class TopNUtil<E extends Comparable<E>> {
   
    private final PriorityQueue<E> priorityQueue;
    private final int n;

    /**
     * 构造 Top-N
     */
    public TopNUtil(int size) {
   
        if (size <= 0) {
   
            throw new IllegalArgumentException("Top-N size must be a positive number");
        }
        this.priorityQueue = new PriorityQueue<>(size);
        this.n = size;
    }

    /**
     * 向 Top-N 中插入元素
     */
    public void add(E e) {
   
        if (priorityQueue.size() < n) {
   
            priorityQueue.add(e);
            return;
        }
        
        E head = priorityQueue.peek();
        if (head != null && e.compareTo(head) <= 0) {
   
            return;
        }
        
        priorityQueue.poll();
        priorityQueue.add(e);
    }

    /**
     * 将 Top-N 转为从大到小排序的 List
     */
    public List<E> toSortedArrayList() {
   
        List<E> tempList = new ArrayList<>(priorityQueue);
        tempList.sort(Collections.reverseOrder());
        return tempList;
    }
}

二、使用示例

class TopNUtilTest {
   

    @Test
    void test() {
   
        List<HerbOrderPriceCountDTO> list = new ArrayList<>();
        TopNUtil<HerbOrderPriceCountDTO> top = new TopNUtil<>(3);

        // 生成 10 个随机的 DoctorFlowDTO
        for (int i = 0; i < 10; i++) {
   
            HerbOrderPriceCountDTO dto = new HerbOrderPriceCountDTO();
            dto.setOrderPriceSum(BigDecimal.valueOf(Math.random() * 100));
            list.add(dto);
        }

        System.out.println("所有的 orderFlow 值:");
        for (HerbOrderPriceCountDTO dto : list) {
   
            System.out.print(dto.getOrderPriceSum());
            System.out.print(" ");
        }
        System.out.println();

        // 将列表中的元素添加到 TopNUtil
        for (HerbOrderPriceCountDTO dto : list) {
   
            top.add(dto);
        }

        // 获取 TopNUtil 中的元素列表
        List<HerbOrderPriceCountDTO> topList = top.toSortedArrayList();

        // 确保列表的大小不超过 3
        assertEquals(3, topList.size());

        // 打印 Top 3 元素的 orderFlow 值
        System.out.println("Top 3 orderFlow 值:");
        for (HerbOrderPriceCountDTO dto : topList) {
   
            System.out.println(dto.getOrderPriceSum());
        }
    }
}

相关推荐

  1. Top-N 工具

    2024-02-22 06:50:01       50 阅读
  2. 14 # 约束

    2024-02-22 06:50:01       53 阅读
  3. TypeScript中对象、

    2024-02-22 06:50:01       56 阅读
  4. <span style='color:red;'>泛</span><span style='color:red;'>型</span>..

    ..

    2024-02-22 06:50:01      61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-22 06:50:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-22 06:50:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-22 06:50:01       87 阅读
  4. Python语言-面向对象

    2024-02-22 06:50:01       96 阅读

热门阅读

  1. MySQL、Redis、Nginx配置优化

    2024-02-22 06:50:01       54 阅读
  2. C++知识点总结(18):排序算法汇总

    2024-02-22 06:50:01       43 阅读
  3. 【笔记】flutter 日历年月日自定义国际化显示

    2024-02-22 06:50:01       47 阅读
  4. 图片数据增强

    2024-02-22 06:50:01       43 阅读
  5. xlua源码分析(六) C#与lua的交互总结

    2024-02-22 06:50:01       59 阅读
  6. Unity3D xLua开发环境搭建详解

    2024-02-22 06:50:01       59 阅读
  7. 雪花算法生成分布式主键ID

    2024-02-22 06:50:01       42 阅读
  8. db-gpt docker部署进坑

    2024-02-22 06:50:01       56 阅读
  9. FastGPT配置文件及OneAPI程序:

    2024-02-22 06:50:01       49 阅读
  10. Rust介绍与开发环境搭建

    2024-02-22 06:50:01       63 阅读