Before converting a Map to a List inwards Java, we should hold upwards really clear most these information structures which are widely used inwards Java. So let's get amongst Map. What is Map? Map is an Interface inwards Java which shop key as well as value object. It's Java representation of pop hash tabular array information construction which allows y'all to search an existing chemical ingredient inwards O(1) time, at the same fourth dimension equally good makes insertion as well as removal easier. We utilization a key object to cry back the value object past times using hashing functionality provided past times Map. As nosotros possess got seen inwards how acquire method of HashMap works, In Java, equals() as well as hashcode() method are an integral portion of storing as well as retrieving object from it. The map allows duplicate values exactly no duplicate keys. Map has its implementation inwards diverse classes similar HashMap, ConcurrentHashMap, as well as TreeMap. The Map interface equally good provides iii collection views, which allow a map's contents to hold upwards viewed equally a develop of keys, collection of values, or develop of key-value mappings.
Now let's sympathise what is a List inwards Java, It is equally good an interface to render an ordered collection based on index. Elements tin hold upwards inserted as well as deleted using positions as well as duplicate elements are allowed inwards the list.
There are multiple implementation of List is available inwards Java e.g. ArrayList, which is based inwards array as well as LinkedList which is based inwards linked listing information structure. Now let's encounter a Java plan which is used to convert a Map to List.
In sec example, nosotros possess got converted all values of Map into List. Map provides a Collection sentiment of all values, because values tin hold upwards duplicates. By using re-create constructor of Collection itself y'all tin easily convert this Collection of Map values into List. In tertiary example, nosotros volition encounter how to convert all entries of HashMap into List. Just similar keys, Map equally good provides a Set sentiment of all entries. Similar to before example, nosotros tin equally good shop all entries into List.
You tin equally good read Core Java Volume i - Fundamentals past times Cay S. Horstmann to larn to a greater extent than most dissimilar collection classes inwards Java.
You tin encounter that how mappings from a Map is converted into a List which is printed inwards the console.
Here is equally good a hierarchical diagram of Java Collection framework, y'all tin encounter Map is non a subclass or subinterface of Collection, they are from the sepearte hierarchy.
That'a all most how to convert Map to List inwards Java. There are many province of affairs when y'all require this e.g. passing Map's content into a legacy or library method which bring List. You tin utilization this event to convert a hash map to listing using its key value ,by using key nosotros brand ArrayList of keys equally good ArrayList of values . If y'all similar to know to a greater extent than most information construction provided past times Java inwards Collection framework, encounter top five information structures from Java API.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part i as well as two
Data Structures inwards Java nine past times Heinz Kabutz
Now let's sympathise what is a List inwards Java, It is equally good an interface to render an ordered collection based on index. Elements tin hold upwards inserted as well as deleted using positions as well as duplicate elements are allowed inwards the list.
There are multiple implementation of List is available inwards Java e.g. ArrayList, which is based inwards array as well as LinkedList which is based inwards linked listing information structure. Now let's encounter a Java plan which is used to convert a Map to List.
Map to List inwards Java
Here is our sample plan to convert a Map to List inwards Java. This event is divided into iii parts; In showtime portion nosotros possess got converted keys of HashMap into List. Map allows y'all to acquire a sentiment of all keys of Map equally Set because duplicate keys are non permitted. If y'all know how to convert Set to List, y'all tin easily convert this Set of keys into List of keys.In sec example, nosotros possess got converted all values of Map into List. Map provides a Collection sentiment of all values, because values tin hold upwards duplicates. By using re-create constructor of Collection itself y'all tin easily convert this Collection of Map values into List. In tertiary example, nosotros volition encounter how to convert all entries of HashMap into List. Just similar keys, Map equally good provides a Set sentiment of all entries. Similar to before example, nosotros tin equally good shop all entries into List.
You tin equally good read Core Java Volume i - Fundamentals past times Cay S. Horstmann to larn to a greater extent than most dissimilar collection classes inwards Java.
Java Program to convert a Map to a List
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Set; /** * Java Program to Convert HashMap into ArrayList inwards Java */ public class MapToList { public static void main(String... args) { HashMap<String, Integer> schoolAgeCriteria = new HashMap<String, Integer>(); // preparing HashMap amongst keys as well as values schoolAgeCriteria.put("NursuryClass historic current criteria", 3); schoolAgeCriteria.put("KinderGarden1 historic current criteria ", 4); schoolAgeCriteria.put("KinderGarden2 historic current criteria ", 5); schoolAgeCriteria.put("PrimarySchool historic current criteria", 6); System.out.println("Size of schoolAgeCriteria Map: " + schoolAgeCriteria.size()); // 1st Example : Converting HashMap keys into ArrayList Set<String> keySet = schoolAgeCriteria.keySet(); List<String> schoolKeyList = new ArrayList<String>(keySet); System.out.println("Size of Key listing from Map: " + schoolKeyList.size()); // impress listing element System.out.println("Printing HashMap keys from converted listing : "); for (String key : schoolKeyList) { System.out.println(key); } // sec Example : Converting HashMap Values into ArrayList Collection<Integer> values = schoolAgeCriteria.values(); List<Integer> schoolValueList = new ArrayList<Integer>(values); System.out.println("Size of Value listing from Map: " + schoolValueList.size()); // impress values from list System.out.println("Printing HashMap values from converted listing :"); for (Integer value : schoolValueList) { System.out.println(value); } // tertiary Example : Converting HashMap into ArrayList using Entry Set Set<Entry<String, Integer>> develop = schoolAgeCriteria.entrySet(); List<Entry<String, Integer>> schoolAgeCriteriaList = new ArrayList<>(set); Iterator<Entry<String, Integer>> it = schoolAgeCriteriaList.iterator(); while (it.hasNext()) { Entry<String, Integer> entry = it.next(); System.out.println("Entry from converted listing : " + entry); } } } Output Size of schoolAgeCriteria Map: 4 Size of Key listing from Map: 4 Printing HashMap keys from converted listing : PrimarySchool historic current criteria KinderGarden1 historic current criteria KinderGarden2 historic current criteria NursuryClass historic current criteria Size of Value listing from Map: 4 Printing HashMap values from converted listing : 6 4 5 3 Entry from converted listing : PrimarySchool historic current criteria=6 Entry from converted listing : KinderGarden1 historic current criteria =4 Entry from converted listing : KinderGarden2 historic current criteria =5 Entry from converted listing : NursuryClass historic current criteria=3
You tin encounter that how mappings from a Map is converted into a List which is printed inwards the console.
Here is equally good a hierarchical diagram of Java Collection framework, y'all tin encounter Map is non a subclass or subinterface of Collection, they are from the sepearte hierarchy.
That'a all most how to convert Map to List inwards Java. There are many province of affairs when y'all require this e.g. passing Map's content into a legacy or library method which bring List. You tin utilization this event to convert a hash map to listing using its key value ,by using key nosotros brand ArrayList of keys equally good ArrayList of values . If y'all similar to know to a greater extent than most information construction provided past times Java inwards Collection framework, encounter top five information structures from Java API.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part i as well as two
Data Structures inwards Java nine past times Heinz Kabutz
No comments:
Post a Comment