public
V put(K key, V value)
Associates
the specified value with the specified key in this map.If the map
previously contained a mapping for the key, the old value is
replaced. Return the previous value associated with key, or null if
there was no mapping for key.
import java.util.*; class HashMapPut{ public static void main(String args[]){ HashMap<Integer, String> myMap; myMap = new HashMap<> (); /* Add Data to myMap */ System.out.print("Putting 100 to myMap "); System.out.println(myMap.put(100, "Hi")); System.out.print("Putting 25 to myMap "); System.out.println(myMap.put(25, "How")); System.out.print("Putting 31 to myMap "); System.out.println(myMap.put(31, "Are")); System.out.print("Putting 100 to myMap "); System.out.println(myMap.put(100, "new String")); System.out.println("Elements in myMap are"); System.out.println(myMap); } }
Output
Putting 100 to myMap null Putting 25 to myMap null Putting 31 to myMap null Putting 100 to myMap Hi Elements in myMap are {100=new String, 25=How, 31=Are}
No comments:
Post a Comment