How to correctly use HashMap?

前端 未结 6 1904
长情又很酷
长情又很酷 2021-01-04 07:28
HashMap savedStuff = new HashMap();
savedStuff.put(\"symbol\", this.symbol); //this is a string
savedStuff.put(\"index\", this.index); //this is an int
6条回答
  •  太阳男子
    2021-01-04 07:45

    This is simple code for use of hashmap. There i will use key as integer and value as string type. Map is very useful when our functionality works on key and values pairs. Below is a simple example of use hashmap. I hope it is very useful for all.

    public class 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);
    
    
        }
    
    }
    

    Reference : create and use of map

提交回复
热议问题