如何正确使用HashMap?

HashMap savedStuff = new HashMap(); savedStuff.put("symbol", this.symbol); //this is a string savedStuff.put("index", this.index); //this is an int 

给我警告:

 HashMap is a raw type. References to generic type HashMap should be parameterized 

我不确定您要做什么,但由于您提供的示例使用硬编码字符串来索引数据,因此您似乎知道要将哪些数据组合在一起。 如果是这种情况,那么Map可能不是一个好的选择。 更好的方法是从通常分组的数据中创建一个类:

 public class SavedStuff { private int index; private String symbol; public SavedStuff(int index, String symbol) { this.index = index; this.symbol = symbol; } public int getIndex() { return index; } public String getSymbol() { return symbol; } } 

这允许您的客户端代码执行此操作:

 SavedStuff savedStuff = ... String symbol = savedStuff.getSymbol(); 

而不是这个:

 Map savedStuff = ... String symbol = savedStuff.get("symbol"); 

前一个例子不那么脆弱,因为你没有用String常量索引数据。 它还为您提供了在分组数据之上添加行为的位置,这使您的代码更加面向对象。

 HashMap savedStuff = new HashMap(); 

当然,在提取元素时,您仍需要小心使用正确的类型。

您需要使用generics ,如下所示:

 Map savedStuff = new HashMap(); 

如果你坚持在同一个地图中使用异构值,那么使用HashMap可能是你能做的最好的事情 – 当你检索它们时,你需要将它们转换成有用的东西(以及你将如何知道它们)将它们投射什么类型……?),但至少你对是类型安全的。

这是一种不同的方法:

一个包含地图并提供不同视图的Helper类:

 public class ValueStore { /** * Inner map to store values. */ private final Map inner = new HashMap(); /** * Returns true if the Value store contains a numeric value for this key. */ public boolean containsIntValue(final String key){ return this.inner.get(key) instanceof Integer; } /** * Returns true if the Value store contains a String value for this key. */ public boolean containsStringValue(final String key){ return this.inner.get(key) instanceof String; } /** * Returns the numeric value associated with this key. * @return -1 if no such value exists */ public int getAsInt(final String key){ final Object retrieved = this.inner.get(key); return retrieved instanceof Integer ? ((Integer) retrieved).intValue() : -1; } /** * Returns the String value associated with this key. * @return null if no such value exists */ public String getAsString(final String key){ final Object retrieved = this.inner.get(key); return retrieved instanceof String ? (String) retrieved : null; } /** * Store a string value. */ public void putAsInt(final String key, final int value){ this.inner.put(key, Integer.valueOf(value)); } /** * Store an int value. */ public void putAsString(final String key, final String value){ this.inner.put(key, value); } /** * Main method for testing. */ public static void main(final String[] args) { final ValueStore store = new ValueStore(); final String intKey = "int1"; final String stringKey = "string1"; final int intValue = 123; final String stringValue = "str"; store.putAsInt(intKey, intValue); store.putAsString(stringKey, stringValue); assertTrue(store.containsIntValue(intKey)); assertTrue(store.containsStringValue(stringKey)); assertFalse(store.containsIntValue(stringKey)); assertFalse(store.containsStringValue(intKey)); assertEquals(123, store.getAsInt(intKey)); assertEquals(stringValue, store.getAsString(stringKey)); assertNull(store.getAsString(intKey)); assertEquals(-1, store.getAsInt(stringKey)); } } 

在检索int值之前,您将检查store.containsIntValue(intKey)的值,并在检索String值之前,检查store.containsStringValue(stringKey) 。 这样你就永远不会检索错误类型的值。

(当然可以扩展到支持其他类型)

这是使用hashmap的简单代码。 在那里我将使用键作为整数和值作为字符串类型。 当我们的function适用于键和值对时,Map非常有用。 下面是一个使用hashmap的简单示例。 我希望它对所有人都非常有用。

公共类CreateHashMap {

 public static void main(String[] args) { Map map = new HashMap(); /* * Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value */ map.put(1,"ankush"); map.put(2, "amit"); map.put(3,"shivam"); map.put(4,"ankit"); map.put(5, "yogesh"); //print hashmap System.out.println("HashMap = "+map); } 

}

参考: 创建和使用地图