首页 > 文档 > HashMap散列映射
2017
07-17

HashMap散列映射

Name

HashMap散列映射

Examples

import java.util.Map;

 

// Note the HashMap’s “key” is a String and “value” is an Integer

HashMap<String,Integer> hm = new HashMap<String,Integer>();

 

// Putting key-value pairs in the HashMap

hm.put(“Ava”, 1);

hm.put(“Cait”, 35);

hm.put(“Casey”, 36);

 

// Using an enhanced loop to interate over each entry

for (Map.Entry me : hm.entrySet()) {

print(me.getKey() + ” is “);

println(me.getValue());

}

 

// We can also access values by their key

int val = hm.get(“Casey”);

println(“Casey is ” + val);


 

Description

HashMap stores a collection of objects, each referenced by a key. This is similar to an Array, only instead of accessing elements with a numeric index, a String is used. (If you are familiar with associative arrays from other languages, this is the same idea.) The above example covers basic use, but there’s a more extensive example included with the Processing examples. In addition, for simple pairings of Strings and integers, Strings and floats, or Strings and Strings, you can now use the simpler IntDict, FloatDict, and StringDict classes.

For a list of the numerous HashMap features, please read the Java reference description.

hashmap 存储一个对象的集合, 其中每一个都由一个键引用。这与数组类似, 只是使用一个字符串, 而不是访问具有数字索引的元素。(如果您熟悉其他语言的关联数组, 则这是相同的想法。上面的示例介绍了基本用法, 但处理示例中包含了一个更广泛的示例。此外, 对于字符串和整数、字符串和浮点、字符串和字符串的简单配对, 现在可以使用更简单的 IntDictFloatDict StringDict 类。

 

有关众多 hashmap 功能的列表, 请阅读 java 参考说明。

Constructor

HashMap<Key, Value>()

HashMap<Key, Value>(initialCapacity)

HashMap<Key, Value>(initialCapacity, loadFactor)

HashMap<Key, Value>(m)

Parameters

Key

Class Name: the data type for the HashMap’s keys类名称: hashmap的关键词的数据类型

Value

Class Name: the data type for the HashMap’s values类名称: hashmap 值的数据类型

initialCapacity

int: defines the initial capacity of the map; the default is 16 int: 定义地图的初始容量;默认值为16

loadFactor

float: the load factor for the map; the default is 0.75浮点: 映射的负荷系数;默认值为0.75

m

Map: gives the new HashMap the same mappings as this Map映射: 赋予新的 hashmap 与此映射相同的映射

Related

IntDict
FloatDict
StringDict



最后编辑:
作者:卡萨布兰卡
卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。