Java

Convert Map to List and list to Map in java 8 using netbeans

Conversion Between Map and List in java

Description:

Convert Map to list in java- Collections such as List and Map in Java can be converted to Map to List and List to Map.

In this article, I will explain how to convert between Map and List with the following contents.

  • Difference between Map and List
  • Convert Map to List in java 8
  • Convert List to Map in java 8

This time, I will explain various methods for converting between Map and List in an easy-to-understand method.




Difference between Map and List

In the first place, Map and List are both types of collections, but what is the difference?

What are the advantages of converting Map to List and List to Map? I will explain in detail.

Difference in features

Maps can define both index keys and their data values. In addition to an integer value, a variable such as a String type can also be specified for the key. You can call the value from the variable specified as the key.

Note that Map keys cannot be duplicated. On the other hand, List automatically generates integer indices in the order in which the elements are stored, and associates them with the values.

Elements of the List specify an index number to call the value. A Map is good if you need to keep a value associated with a key.

Also, a List can be sorted by the value of the elements, but Map cannot be sorted as it is.

You need to convert Map keys and values ​​to Lists and arrays. A List is a good choice if you need the order of the elements. Let’s refer to the explanation from now on and convert it according to the purpose!



The difference in definition (HashMap and ArrayList)

Map and List are interfaces, so you need to use another class to instantiate them.

Use a class that implements an interface, such as the HashMap class for Map and the ArrayList class for List.

Use these to define:

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

List<String> list = new ArrayList<>();

Both Map and List are interfaces and cannot be instantiated, but objects can be declared. The object stores an instance of the class that implements each interface.

In this example, the Map key is specified as String type and the value is specified as Integer type. The type is specified using the class type.

Note that primitive types such as int cannot be used.

Convert Map to List in java

Here, I will explain how to convert Map keys to Lists and how to convert Map values ​​to Lists!



Convert Map key to List in java 8

Use the keySet method to convert a Map key to a List. Below, I describe how to convert Map keys to List.

package com.mycompany.javabasics;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        //Map declaration

        Map<String, Integer> langData = new HashMap<>();

        langData.put(“Java”, 1);

        langData.put(“C#”, 2);

        langData.put(“Android”, 3);

        langData.put(“VB.net”, 4);

        langData.put(“C++”, 5);

        langData.put(“Arduino”, 6);

        //here i declare a list to get the value of the key using keyset method

        List<String> list = new ArrayList<>(langData.keySet());

        System.out.println(list);

    }

}

Output:

Convert map to list in java

The sample declares a Map class and uses the put method to set the key and value to the variable map.

Next, we declare a List class and use the keySet method to store the Map key in the variable list. If you check the contents of the List, you can see that all the keys added in Map have been acquired.



Convert Map value to List in java 8

To get the value, use the values ​​method.

To convert the value of Map to List, change the langData.keySet()); to langData.values()); in the list statement and make sure pass the integer parameter instead of String in list angle brackets. Below, i describe how to convert Map values to List.

package com.mycompany.javabasics;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        //Map declaration

        Map<String, Integer> langData = new HashMap<>();

        langData.put(“Java”, 1);

        langData.put(“C#”, 2);

        langData.put(“Android”, 3);

        langData.put(“VB.net”, 4);

        langData.put(“C++”, 5);

        langData.put(“Arduino”, 6);

        //here i declare a list to get the value of the key using keyset method

        List<Integer> list = new ArrayList<>(langData.values());

        System.out.println(list);

    }

}

Output:

Convert map to list in java

The difference between getting the value of the key is that the keySet method is used to get the key when converting Map to List, while the values ​​method is used to get the value.

If you check the contents of the List, you can see that all the values ​​added in Map have been acquired.




Convert List to Map in java 8

Here, I will explain how to convert List to map in java using netbeans

Convert List value to Map key in java 8

Below is a sample code that sets the List value to the Map key.

package com.mycompany.javabasics;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        // List declaration

        List<Integer> list = new ArrayList<>();

        list.add(1);

        list.add(2);

        list.add(3);

        list.add(4);

        list.add(5);

        list.add(6);

        //Map declaration

        Map<Integer, String> MapData = new HashMap<>();

        for (Integer num : list) {

            // Add List keys and values to Map

            MapData.put(num, “Java”);

        }

        System.out.println(MapData);

    }

}

Output:

Convert map to list in java

First, I declare a List class and add a value to the variable list.

Next, i declare a Map class, use an extended for a statement to loop through the list, and store the values ​​of the List as the keys of the Map using the put method.



Convert List value to Map value in java 8

Below is a sample code that sets the value of List to the value of Map.

package com.mycompany.javabasics;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

/**

 *

 * @author Fawadkhan

 */

public class MapExamples {

    public static void main(String[] args) {

        // List declaration

        List<String> Langlist = new ArrayList<>();

        Langlist.add(“Java”);

        Langlist.add(“C++”);

        Langlist.add(“C#”);

        Langlist.add(“Android”);

        Langlist.add(“Arduino”);

        Langlist.add(“Vb.net”);

        Langlist.add(“Python”);

        //Map declaration

        Map<Integer, String> MapData = new HashMap<>();

        int i = 1;

        for (String str : Langlist) {

            // Add List values to Map

            MapData.put(i, str);

            i++;

        }

        System.out.println(MapData);

    }

}

Output:

Convert map to list in java

First, I declare a List class and add a value to the variable list. Next, I declare a Map class, use an extended for a statement to loop through the list, and use the put method to store the elements of the Map.

Regarding the Map key, the int type variable i is declared, incremented in a loop, and set as the Map key.

If you check the output, you can see that the value of the variable map has been obtained.



summary

I explained how to Convert Map to List and list to Map in java 8. And I used these in practical examples in a very easy manner to understand, so please take this opportunity to remember them.

If you forget how to convert collections such as Maps and Lists, remember this article.

Fawad

Shahzada Fawad

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button