package com.payu.Payu;
import java.util.*;
public class HashMap_Example {
public static void main(String[] args) {
// Creating an empty HashMap
HashMap<Integer, String> hashmap = new HashMap<Integer, String>();
// Mapping string values to int keys
hashmap.put(10, "HashMap");
hashmap.put(15, "4");
hashmap.put(25, "You");
// Displaying the HashMap
System.out.println("Initial Mappings are: " + hashmap);
// Inserting existing key along with new value
// return type of put is type of values i.e. String and containing the old value
String returned_value = hashmap.put(10, "abc");
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Inserting new key along with new value
// return type of put is type of values i.e. String ; since it is new key ,return value will be null
returned_value = hashmap.put(20, "abc");
// Verifying the returned value
System.out.println("Returned value is: " + returned_value);
// Displayin the new map
System.out.println("New map is: " + hashmap);
}
}
输出 :-
初始映射为:{25=You, 10=HashMap, 15=4}
返回值为:HashMap
返回值为:null
新映射为:{20=abc, 25=You, 10=abc, 15=4}