创建< char,int>的无序映射在java中 [英] Creating an unordered map of <char, int> in java

查看:219
本文介绍了创建< char,int>的无序映射在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要有一个多重的字符集,其中添加一个重复的字符增加基数1,并且字符的多样性不应该大大增加对象占用的内存。

So I need to have a some sort of multiset of characters, where adding a duplicate character increases the cardinality by 1, and the multiplicity of characters should not drastically increase the memory that the object takes up.

这将使用某种地图来实现,其中字符是键,它们保存表示该字符在该集合中表示的数字的值。

This will be implemented with some sort of map where characters are keys, that hold a value representing the number of that character is represented in the set.

但是,我很难找出哪个集合最适合这个(我在看着hashmap)以及如何声明这个数据类型。我正在做这样的事情

However, I'm struggling to figure out which collection would be best for this (I was looking at hashmap) and how to declare this data type. I was doing something like this

Map m = new HashMap(char, int);

但是上面是一个不正确的声明,我不知道如何精确地逼近。 / p>

But the above is an incorrect declaration, and I'm not sure how to exactly approach this.

推荐答案

尝试此声明:

Map<Character, Integer> m = new HashMap<Character, Integer>();

您可以这样添加字符:

char c = //...;
if (m.containsKey(c))
    m.put(c, m.get(c) + 1);
else
    m.put(c, 1);

这篇关于创建&lt; char,int&gt;的无序映射在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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