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 :: Syntax of how to create ArrayList in Java 
Java :: is it possible to quick sort a string in java 
Java :: java abstraction 
Java :: como crear un array list en java 
Java :: java bigdecimal 
Java :: convert object array to int array java 
Java :: print all numbers from 1 to n java 
Java :: print anything in java 
Java :: char array to string in java 
Java :: android view set padding programmatically 
Java :: how to convert 2d to 1d array in java 
Java :: how to get string input line in java 
Java :: Java How to use Map? 
Java :: java set classpath 
Java :: Java Create a BufferedOutputStream 
Java :: java static variable 
Java :: java convert ip to long 
Java :: run webgoat using docker 
Java :: alphabet n vowelin java 
Java :: how to set path for java in windows 10 
Java :: Java Creating a Java LinkedList 
Java :: java how to make a parameter optional 
Java :: Java boolean Keyword 
Java :: jackson deserialization fail-on-unknown-properties true 
Java :: how to merge two arrays in java 
Java :: how to hash string in java 
Java :: contient java 
Java :: how to create dynamic string array in java 
Java :: java clear scanner 
Java :: méthode retourne nom classe java 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =