Java

How to declare and initialize a Map (HashMap) using NetBeans

Map declare and initialize in java with examples

Description:

Ever wanted to use Maps in Java? If you’re new to Map, you may be confused about how to declare and initialize it. This time, I will explain how to declare and initialize a Map!

In this article, you will learn

  • What is Map
  • How to declare Map
  • How to add elements with put
  • How to add and initialize values ​​at the same time as the declaration
  • How to Constantine Map with the unmodifiable Map method

From basic content such as, we will also explain how to use applied. This time, to remember these methods, I will explain how to declare and initialize Map in an easy-to-understand manner!




What is Map:

A Map consists of two elements.

  • The first is a name called “key”
  • The second is “value”

Simply put, a “key” can be thought of as a name attached to a value. A characteristic of Map is that each value has a “key”, and a “key” and a “value” are paired. Arrays and Lists specify values ​​with index numbers, but Maps specify values ​​with keys.

How to Declare a Map and add elements with put method:

First, I will explain how to declare a Map using new and how to add elements with the put method. Let’s check the following program to learn how to declare Map and add elements.

package com.mycompany.javabasics;

import java.util.HashMap;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        Map<String, String> Data = new HashMap<>();

        Data.put(“Website”, “programmingdigest.com”);

        Data.put(“CEO”, “Fawad khan”);

        for (Map.Entry<String, String> MapData : Data.entrySet()) {

            System.out.println(MapData.getKey() + ” : ” + MapData.getValue());

        }

    }

}

Output:

how to declare and initialize a map

Here, after declaring a Map, we are adding keys and values ​​using the put method. Now you can declare a Map and add elements!



How to add and initialize values ​​at the same time as the declaration

Earlier, I explained how to add elements with the put method after declaring a Map, but here I will explain how to add values ​​and initialize them at the same time as declaring it.

How to initialize an anonymous class

One way to batch add and initialize elements in a Map is to use an anonymous class. Note that you cannot omit the type inside the angle operator (<>). Let’s check the following program and how to initialize in an anonymous class.

package com.mycompany.javabasics;

import java.util.HashMap;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        Map<String, String> Data = new HashMap<String, String>() {

            {

                put(“Website”, “programmingdigest.com”);

                put(“CEO”, “Fawad khan”);

            }

        };

        for (Map.Entry<String, String> MapData : Data.entrySet()) {

            System.out.println(MapData.getKey() + ” : ” + MapData.getValue());

        }

    }

}

Output:

how to declare and initialize a map

I was able to initialize the Map by using an anonymous class like this!



How to use static initializer

Here I will explain how to initialize using a static initializer. A static initializer is used to initialize class variables within a class. Let’s see how to use the static initializer in the following program.

package com.mycompany.javabasics;

import java.util.HashMap;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    private static Map<String, String> Data = new HashMap<>();

    static {

        Data.put(“Website”, “programmingdigest.com”);

        Data.put(“CEO”, “Fawad khan”);

    }

    public static void main(String[] args) {

        for (Map.Entry<String, String> MapData : Data.entrySet()) {

            System.out.println(MapData.getKey() + ” : ” + MapData.getValue());

        }

    }

}

Output:

how to declare and initialize a map

I was able to initialize the Map by using a static initializer like this!

Is this way of writing a declaration of a map

Is it possible to initialize a Map in the following way?

Map<String, String> map = new Map<>();

You can’t initialize it because you can’t create an instance with this way of writing. Remember to store a HashMap object in a Map-type variable and use it as follows.

Map<String, String> map = new HashMap<>();

In addition to HashMap, TreeMap and LinkedHashMap may be used depending on the purpose, but speaking of Map, HashMap is used.



constant Map with the unmodifiable map method

Finally, I will explain how to treat Map as a constant. If you want to treat an int type as a constant, add the final modifier to make it a constant whose value cannot be changed.

However, in the case of Map, elements can be added even with the final modifier, so we use the unmodifiable Map method to make it constant. Let’s check it with the following program.

package com.mycompany.javabasics;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    private static Map<String, String> Data;

    static {

        Map<String, String> map = new HashMap<>();

        map.put(“Website”, “programmingdigest.com”);

        map.put(“CEO”, “Fawad khan”);

        Data = Collections.unmodifiableMap(map);

    }

    public static void main(String[] args) {

        for (Map.Entry<String, String> MapData : Data.entrySet()) {

            System.out.println(MapData.getKey() + ” : ” + MapData.getValue());

        }

    }

}

Output:

how to declare and initialize a map

Here, the Map initialized by the static initializer is made constant using the unmodifiable Map method. By using the unmodifiable Map method like this, we were able to create a Map that does not allow the value to be changed!




Summary

How was it?

This time, I explained how to declare and initialize a Map (HashMap). Don’t forget that when using Map as a constant, you need to use the unmodifiable Map method as well as the final modifier.

If you forgot how to add values ​​at the same time as initializing a Map, or how to make a Map constant, check out this article!

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button