How to Convert Two Arrays Containing Keys and Values to HashMap in Java? Last Updated : 17 Dec, 2020 Comments Improve Suggest changes Like Article Like Report HashMap is part of the Collections framework of java. It stores the data in the form of key-value pairs. These values of the HashMap can be accessed by using their respective keys or the key-value pairs can be accessed using their indexes (of Integer type). HashMap is similar to Hashtable in java. The main difference between HashTable and HashMap is that Hashtable is synchronized but HashMap is not. Also, a HashMap can have one null key and any number of null values. The insertion order s not preserved in the HashMap. In a HashMap, keys and values can be added using the HashMap.put() method. We can also convert two arrays containing keys and values into a HashMap with respective keys and values. Example: keys = {1,2,3,4,5} values = {"Welcome","To","Geeks","For","Geeks"} HashMap = {1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks} Code Java // Java program to convert two arrays containing keys and // values into a HAshMap import java.util.*; import java.io.*; class GFG { public static HashMap map(Integer[] keys, String[] values) { // two variables to store the length of the two // given arrays int keysSize = keys.length; int valuesSize = values.length; // if the size of both arrays is not equal, throw an // IllegalArgumentsException if (keysSize != valuesSize) { throw new IllegalArgumentException( "The number of keys doesn't match the number of values."); } // if the length of the arrays is 0, then return an // empty HashMap if (keysSize == 0) { return new HashMap(); } // create a new HashMap of the type of keys arrays // and values array HashMap<Integer, String> map = new HashMap<Integer, String>(); // for every key, value for (int i = 0; i < keysSize; i++) { // add them into the HashMap by calling the // put() method on the key-value pair map.put(keys[i], values[i]); } // return the HashMap return map; } // Driver method public static void main(String[] args) { // create an array for keys Integer[] keys = { 1, 2, 3, 4, 5 }; // create an array for value String[] values = { "Welcome", "To", "Geeks", "For", "Geeks" }; // call the map() method over the keys[] array and // values[] array Map m = map(keys, values); // print the returned map System.out.println(m); } } Output{1=Welcome, 2=To, 3=Geeks, 4=For, 5=Geeks} Comment More infoAdvertise with us Next Article How to Convert Two Arrays Containing Keys and Values to HashMap in Java? ushashree Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-HashMap +1 More Practice Tags : Java Similar Reads How to Convert a HashTable to Other Collections Types in Java? In Java, a Hashtable is a data structure that stores the data in the form of key and value pairs. And each key is mapped to a specific value. It implements the Map interface. HashTable provides a simple way to access the data using the unique keys. In this article, we will learn how to convert a Has 3 min read How to Convert ArrayList to HashMap Before Java 8? ArrayList is a resizable array. It is under the java package java.util. It gives dynamic arrays in Java. ArrayList is useful in programs where a lot of changes in the array are required but these are also slower than the standard arrays. The elements in an ArrayList can be added and removed whenever 4 min read How to Convert TreeMap to an ArrayList in Java? TreeMap is a part of the Java Collection framework. Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class. It provides an efficient means of storing key-value pairs in sorted order. Java TreeMap contains only unique elements. It cannot 4 min read How to Convert all LinkedHashMap Values to a List in Java? The task is to convert all LinkedHashMap values to a list in java. LinkedHashMap is an implementation of a Map. The Map and List are two different data structures. The Map stores key-value pairs while the List is an ordered collection of elements. To convert all values of the LinkedHashMap to a List 2 min read How to Merge Two HashMaps in Java while Handling Conflicts? HashMap is a part of the collection framework found in Java. It is found in the java.util package and provides the basic implementation of the Map interface of Java. Conflict handling is very important in HashMap as if they are ignored the code will not work properly. In this article, we will learn 3 min read Java Program to Sort a HashMap by Keys and Values HashMap<K, V> is a Java Collection and is a part of java.util package. It provides the basic implementation of the Map interface of Java. It stores the data in the form of Key, Value pairs, where the keys must be unique but there is no restriction for values. If we try to insert the duplicate 3 min read Converting ArrayList to HashMap in Java 8 using a Lambda Expression A lambda expression is one or more line of code which works like function or method. It takes a parameter and returns the value. Lambda expression can be used to convert ArrayList to HashMap. Syntax: (parms1, parms2) -> expressionExamples: Input : List : [1="1", 2="2", 3="3"] Output: Map : {1=1, 2 min read How to Convert Comma Separated String to HashSet in Java? Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which 2 min read Getting Set View of Keys from HashMap in Java The HashMap class of Java provides the functionality of the hash table data structure. This class is found in the java.util package. It implements the Map interface. It stores elements in (Key, Value) pairs and you can access them by an index of another type (e.g. Integer/String). Here, keys are use 2 min read Like