Click here to watch in Youtube :
https://www.youtube.com/watch?v=oNoKvbrGF98&list=UUhwKlOVR041tngjerWxVccw
HashMapExample.java
Output
https://www.youtube.com/watch?v=oNoKvbrGF98&list=UUhwKlOVR041tngjerWxVccw
HashMapExample.java
import java.util.HashMap; /* * Example of put(K key,V value) method. */ public class HashMapExample { public static void main( String[] args ) { HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); /* * 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. * * Returns: the previous value associated with key, or null if there was * no mapping for key. (A null return can also indicate that the map * previously associated null with key.) */ String value = hashMap.put(1, "Apple"); System.out.println("Previous value associated with key '1' : " + value); System.out.println("hashMap : " + hashMap + "\n"); value = hashMap.put(1, "Ball"); System.out.println("Previous value associated with key '1' : " + value); System.out.println("hashMap : " + hashMap); } }
Previous value associated with key '1' : null hashMap : {1=Apple} Previous value associated with key '1' : Apple hashMap : {1=Ball}
https://sites.google.com/site/javaee4321/java-collections/HashMapDemoPutReturnValue.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment