Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java add to map

HashMap<String, String> map = new HashMap<>();
map.put("key", "value");
Comment

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 :: interface declaration in java 
Java :: static method in non static class java 
Java :: java programming problems 
Java :: java put() method 
Java :: my canvas java 
Java :: how to create microservices architecture with spring boot 
Java :: join strings in java 
Java :: how to sort linked list in java 
Java :: deep content 
Java :: Reconstruct Binary Tree With Levelorder And Inorder leetcode 
Java :: enter a word and print letters java 
Java :: multiple inputs java 
Java :: edit text on 2sec change andropid 
Java :: java and python begineers mcq with answers 
Java :: generate random color java 
Java :: java inser at index 
Java :: url to json 
Java :: how to display an integer in a textfield in java 
Java :: textfield invisible java 
Java :: preparestatement method in java 
Java :: 2d matrix multiplication 
Java :: action listener for button to close window java 
Java :: how to cut a certion part from a string in java 
Java :: android string animation 
Java :: code for working clock in java eclipse 
Java :: @Bean public ModelMapper modelMapper() { ModelMapper modelMapper = new ModelMapper(); modelMapper.getConfiguration() .setMatchingStrategy(MatchingStrategies.STRICT); } 
Java :: Get and set method uml 
Java :: java static nested class 
Java :: reader java 
Java :: ERROR: Failed to resolve: com.github.HaarigerHarald:android-youtubeExtractor:v1.7.0 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =