Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Hashmap Add Elements

import java.util.*;  
public class hashmapeg3{  
 public static void main(String args[]){  
   HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap    
   
   System.out.println("Initial HashMap: " + map);
   
   map.put(1,"Cat");  //Put elements in Map  
   map.put(2,"Dog");    
   map.put(3,"Markhor");   
   map.put(4,"Peacock");   
       
   System.out.println("HashMap after put(): ");  
   for(Map.Entry m : map.entrySet()){    
    System.out.println(m.getKey()+" "+m.getValue());    
   }  
}  
}
Comment

Java Add elements to a HashMap

import java.util.HashMap;

class Main {
  public static void main(String[] args) {

    // create a hashmap
    HashMap<String, Integer> numbers = new HashMap<>();

    System.out.println("Initial HashMap: " + numbers);
    // put() method to add elements
    numbers.put("One", 1);
    numbers.put("Two", 2);
    numbers.put("Three", 3);
    System.out.println("HashMap after put(): " + numbers);
  }
}
Comment

Add Elements in java map

// Java program to demonstrate
// the working of Map interface

import java.util.*;
class Main {
	public static void main(String args[])
	{
		// Default Initialization of a
		// Map
		Map<Integer, String> hashmap1 = new HashMap<>();

		// Initialization of a Map
		// using Generics
		Map<Integer, String> hashmap2= new HashMap<Integer, String>();

		// Inserting the Elements
		hashmap1.put(1, "Apple");
		hashmap1.put(2, "Banana");
		hashmap1.put(3, "Mango");

		hashmap2.put(new Integer(1), "Mango");
		hashmap2.put(new Integer(2), "Apple");
		hashmap2.put(new Integer(3), "Banana");

		System.out.println(hashmap1 +"
");
		System.out.println(hashmap2);
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: meaning of instantiated in java 
Java :: how to get length of 2d array java 
Java :: How to launch app in your android mobile phone 
Java :: generate hash in java 
Java :: how to use java 
Java :: each loop in java 
Java :: intent class for url and phone and others 
Java :: array to array list java 
Java :: java comment 
Java :: how do you handle exceptions in java 
Java :: how to compare two strings in java 
Java :: runtime exception in java 
Java :: advantages of using java 
Java :: round int java 
Java :: spring security controlleradvice 
Java :: calling a void method java 
Java :: run java class file 
Java :: Print Positives of array 
Java :: reading 2d array in java 
Java :: Does JVM create object of Main class (the class with main())? 
Java :: android java close keyboard 
Java :: android system navigation back bar hide 
Java :: java get first not null element 
Java :: iterator java8 
Java :: java GLIBC 2 34 missing 
Java :: How do I retrieve editors registered for a certain file extension in Eclipse? 
Java :: convert boolean to Boolean class 
Java :: javax.servlet.Filter 
Java :: java reverse a word 
Java :: What is accept() method in networking 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =