Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

new hashmap java

Map<String, String> myMap = new HashMap<String, String>() {{
        put("a", "b");
        put("c", "d");
    }};
Comment

declare hashmap java

//remember to first import java.util.*; first

//you can swap out string or integer for other data types
Map<String, Integer> d = new HashMap<>();
Comment

declare a hashmap in java

   HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap    
Comment

Create HashMap in Java

import java.util.HashMap;

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

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

    // add elements to hashmap
    languages.put("Java", 8);
    languages.put("JavaScript", 1);
    languages.put("Python", 3);
    System.out.println("HashMap: " + languages);
  }
}
Comment

new hashmap java


// this works for up to 10 elements:
Map<String, String> test1 = Map.of(
    "a", "b",
    "c", "d"
);

// this works for any number of elements:
import static java.util.Map.entry;    
Map<String, String> test2 = Map.ofEntries(
    entry("a", "b"),
    entry("c", "d")
);

Comment

PREVIOUS NEXT
Code Example
Java :: java stream min 
Java :: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException 
Java :: java how to check string is number 
Java :: get text from a jtextfield 
Java :: java divisible by operator 
Java :: How to remove backstack fragment 
Java :: how to check whether a character is alphabet or not in java 
Java :: set remove element java 
Java :: string to bigdecimal 
Java :: Exception in thread "main" java.util.zip.ZipException: error 
Java :: java arraylist print 
Java :: Get the last Friday of a month in Java 
Java :: how to add two numbers in java 
Java :: java to check if its a number scanner 
Java :: java singleton implementation 
Java :: java triangle 
Java :: get timezone offset java 
Java :: java 8 map foreach 
Java :: print list array 
Java :: android studio create a Uri 
Java :: max int in java 
Java :: cls java 
Java :: generatedvalue spring boot 
Java :: How to find the prime factors of a number in Java? 
Java :: java if or 
Java :: java count to 10 
Java :: how to make a loop in java 
Java :: sort a string array java 
Java :: native insert query in jpa repository 
Java :: java mockito any string import 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =