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:

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.

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 of the map.

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.

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.

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.

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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button