Map以按键/数值对的形式存储数据,和数组非常相似,在数组中存在的索引,它们本身也是对象。
Map的接口
Map---实现Map
Map.Entry--Map的内部类,描述Map中的按键/数值对。
SortedMap---扩展Map,使按键保持升序排列
关于怎么使用,一般是选择Map的子类,而不直接用Map类。
下面以HashMap为例。
public static void main(String args[])
{
HashMap hashmap = new HashMap();
hashmap.put("Item0", "Value0");
hashmap.put("Item1", "Value1");
hashmap.put("Item2", "Value2");
hashmap.put("Item3", "Value3");
Set set = hashmap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()
{
Map.Entry mapentry = (Map.Entry) iterator.next();
System.out.println(mapentry.getkey() + "/" + mapentry.getValue());
}
}
注意,这里Map的按键必须是唯一的,比如说不能有两个按键都为null。
如果用过它,就会知道它的用处了。
Map的接口
Map---实现Map
Map.Entry--Map的内部类,描述Map中的按键/数值对。
SortedMap---扩展Map,使按键保持升序排列
关于怎么使用,一般是选择Map的子类,而不直接用Map类。
下面以HashMap为例。
public static void main(String args[])
{
HashMap hashmap = new HashMap();
hashmap.put("Item0", "Value0");
hashmap.put("Item1", "Value1");
hashmap.put("Item2", "Value2");
hashmap.put("Item3", "Value3");
Set set = hashmap.entrySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()
{
Map.Entry mapentry = (Map.Entry) iterator.next();
System.out.println(mapentry.getkey() + "/" + mapentry.getValue());
}
}
注意,这里Map的按键必须是唯一的,比如说不能有两个按键都为null。
如果用过它,就会知道它的用处了。
资料: