在 java 中创建 <char, int> 的无序映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13595497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an unordered map of <char, int> in java
提问by Aerlusch
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.
因此,我需要某种多组字符,其中添加重复字符会将基数增加 1,并且字符的多样性不应显着增加对象占用的内存。
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.
这将通过某种映射来实现,其中字符是键,其中包含一个表示该字符在集合中表示的数量的值。
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
但是,我正在努力弄清楚哪个集合最适合这个(我正在查看 hashmap)以及如何声明这种数据类型。我正在做这样的事情
Map m = new HashMap(char, int);
But the above is an incorrect declaration, and I'm not sure how to exactly approach this.
但以上是一个不正确的声明,我不确定如何准确地解决这个问题。
回答by arshajii
Try this declaration:
试试这个声明:
Map<Character, Integer> m = new HashMap<Character, Integer>();
You can then add characters as such:
然后,您可以添加这样的字符:
char c = //...;
if (m.containsKey(c))
m.put(c, m.get(c) + 1);
else
m.put(c, 1);
回答by akuhn
I would implement it using int[]
(for ASCII) or int[][]
for unicode. Given the memory footprint of storing boxed integers in a hash map with all the hashing conflicts introduced by using numbers, and characters are nothing but numbers, as keys.
我会使用int[]
(用于 ASCII)或int[][]
用于 unicode来实现它。考虑到将装箱整数存储在哈希映射中的内存占用以及使用数字引入的所有哈希冲突,并且字符只是数字作为键。
public class CharacterBag {
private int[][] data = new int[255][];
public void add(char ch) {
int[] bin = data[ch >> 8];
if (bin == null) bin = data[ch >> 8] = new int[255];
bin[ch & 0xFF]++;
}
public int frequency(char ch) {
int[] bin = data[ch >> 8];
if (bin == null) return 0;
return bin[ch & 0xFF];
}
}
This starts with a memory footprint of zero, and adds 2K for each unicode page. Typically text uses characters from one or two unicode pages.
这从零内存占用开始,并为每个 unicode 页增加 2K。通常文本使用来自一两个 Unicode 页面的字符。
In comparison using HashMap will have the whole overhead of storing boxed primitives in a list of linked lists. For each character there will an object of the Entry
class with two pointers pointing to a boxed key and a linked list object with all its fields and which holds an object of class Node
with a forward and a backward pointer and which has a pointer to the boxed integer … shall I go on? ;)
相比之下,使用 HashMap 将具有将装箱原语存储在链表列表中的全部开销。对于每个字符,将有一个Entry
类的对象,其中两个指针指向一个装箱键,一个链表对象及其所有字段,其中包含一个Node
具有向前和向后指针的类对象,以及一个指向装箱整数的指针……我要继续吗?;)
回答by cohadar
Map<Character, Integer> charCount = new HashMap<Character, Integer>();
public void addCharacter(char c) {
Integer value = charCount.get(c);
if (value == null) {
charCount.put(c, 1);
} else {
charCount.put(c, value + 1);
}
}
回答by KyelJmD
Java collections does not allow you to create a collection of primitive types, rather you should use their respective wrapper class (eg The wrapper class for int is Integer).
Java 集合不允许您创建原始类型的集合,而应该使用它们各自的包装类(例如 int 的包装类是 Integer)。
Map<Character,Integer> hashMap = new HashMap<Character,Integer>();
This how you declare a map. for more examples and explanation please look here
这就是您声明地图的方式。更多例子和解释请看这里
回答by Mr_Hmp
Best way according to me is to use the constructor:
我认为最好的方法是使用构造函数:
Map<Key, Value> orgMap = new HashMap<Key, Value>();
Map<Key, Value> copyMap;
copyMap= new HashMap<Key, Value>(map);
Keep it simple.
把事情简单化。