Java中的排序集合 [英] Sorted collection in Java

查看:23
本文介绍了Java中的排序集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 初学者.请建议可以/应该使用哪些集合来维护 Java 中的排序列表.我尝试过 MapSet,但它们不是我想要的.

I'm a beginner in Java. Please suggest which collection(s) can/should be used for maintaining a sorted list in Java. I have tried Map and Set, but they weren't what I was looking for.

推荐答案

这来得太晚了,但是 JDK 中有一个类只是为了有一个排序列表.它被命名为(与其他 Sorted* 接口有点乱)java.util.PriorityQueue".它可以对 Comparable 或使用 Comparator 进行排序.

This comes very late, but there is a class in the JDK just for the purpose of having a sorted list. It is named (somewhat out of order with the other Sorted* interfaces) "java.util.PriorityQueue". It can sort either Comparable<?>s or using a Comparator.

与使用 Collections.sort(...) 排序的 List 的区别在于,这将始终保持偏序,O(log(n)) 插入性能,通过使用堆数据结构,而在已排序的 ArrayList 中插入将是 O(n)(即,使用二进制搜索和移动).

The difference with a List sorted using Collections.sort(...) is that this will maintain a partial order at all times, with O(log(n)) insertion performance, by using a heap data structure, whereas inserting in a sorted ArrayList will be O(n) (i.e., using binary search and move).

然而,与 List 不同的是,PriorityQueue 不支持索引访问(get(5)),唯一的方法是访问堆中的项目是将它们取出,一次一个(因此名称为PriorityQueue).

However, unlike a List, PriorityQueue does not support indexed access (get(5)), the only way to access items in a heap is to take them out, one at a time (thus the name PriorityQueue).

这篇关于Java中的排序集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆