首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过比较键的值在HashMap中添加BinaryHeap键?

如何通过比较键的值在HashMap中添加BinaryHeap键?
EN

Stack Overflow用户
提问于 2022-07-17 13:47:26
回答 1查看 61关注 0票数 0

我有一个HashMap<&str,i32>,用来存储不同单词的频率。我必须返回最频繁的字符串。在使用Java之前,我已经完成了这个问题,但是我无法想出如何在生锈中完成这个问题。

代码语言:javascript
运行
复制
//my hashmap 
Map<String, Integer> map = new HashMap<>();
//my priority queue
PriorityQueue<Map.Entry<String, Integer>>pq = new PriorityQueue<>(
            (a,b) -> a.getValue() == b.getValue() ? b.getKey().compareTo(a.getKey()) :a.getValue() - b.getValue()
        );

我创建了一个PriorityQueue,并通过了我所需要的比较器,我不知道如何在生锈时做同样的事情。

地图{“你好”:2,“世界”:3,“程序”:1}

我希望BinaryHeap看起来像这样:堆{“世界”,“你好”,“程序”};

EN

Stack Overflow用户

发布于 2022-07-17 14:23:43

通常,比较函数(Ord impl)不可能引用它不包含的数据,参见这个问题。在这种情况下,由于不希望在插入后更改密钥的优先级,所以可以插入(count, T)的元组。可以将值包装在std::cmp::Reverse中,以将默认的最大堆转换为最小堆。

下面是从字符串中提取前5个最频繁字符的代码,使用HashMap进行计数,然后使用BinaryHeap提取顶级值:

代码语言:javascript
运行
复制
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;

fn main() {
    let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

    let mut map = HashMap::new();
    for char in text.chars() {
        *map.entry(char).or_insert(0) += 1;
    }

    let mut heap = BinaryHeap::new();

    for (char, count) in map {
        heap.push(Reverse((count, char)));
        if heap.len() > 5 {
            heap.pop(); // Remove the least frequent.
        }
    }

    dbg!(heap
        .into_sorted_vec()
        .into_iter()
        .map(|Reverse(data)| data)
        .collect::<Vec<_>>());
}

输出:

代码语言:javascript
运行
复制
[src/main.rs:22] heap.into_sorted_vec().into_iter().map(|Reverse(data)|
            data).collect::<Vec<_>>() = [
    (
        90,
        ' ',
    ),
    (
        59,
        'e',
    ),
    (
        43,
        't',
    ),
    (
        39,
        's',
    ),
    (
        38,
        'n',
    ),
]

游乐场

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73012310

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档