简体   繁体   English

以排序方式添加到哈希表(Java)

[英]Add to hashmap in sorted fashion (Java)

Hashmaps take O(1) to get stuff or remove it. 哈希图使用O(1)来获取或删除它。 If I want to sort it it will take nlogn time with Collections.sort(). 如果我想对其进行排序,则需要使用Collections.sort()进行登录。 If I use a treemap instead it sorts them while adding them so I don't have to spend nlogn sorting but it takes nlogn finding stuff. 如果我改用树形图,则在添加它们时对它们进行排序,因此我不必花费nlogn进行排序,但需要nlogn来查找内容。 Hence the question is can I manually control the hashmap put method so that it doesn't use the hashcode way but instead sorts them with comparable? 因此,问题是我可以手动控制hashmap put方法,以使其不使用哈希码方式,而是以可比的方式对它们进行排序吗? I am seeking O(1) for a program that uses hashmaps to do many insertions and removals and also much sorting. 我正在为使用哈希图执行许多插入和删除以及大量排序的程序寻求O(1)。

Sorting n items will (in almost every case) take O(n log n) time. 排序n项目(几乎在每种情况下)将花费O(n log n)时间。 This is a provable fact. 这是可以证明的事实。 See Wikipedia on Sorting for a lot of information on the bounds on sorting algorithms. 有关排序算法范围的大量信息,请参见Wikipedia on Sorting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM