How To Convert A Map To A Listing Inwards Coffee Example
Sunday, June 3, 2018
Add Comment
Map as well as List are 2 mutual information structures available inwards Java as well as inwards this article nosotros volition run into how tin nosotros convert Map values or Map keys into List inwards Java. The primary divergence betwixt Map (HashMap, ConcurrentHashMap or TreeMap) as well as List is that Map holds 2 objects commutation as well as value land List only holds 1 object which itself is a value. Key inwards hashmap is only an addon to detect values, thence y'all tin only alternative the values from Map as well as practice a List out of it. The map inwards Java allows duplicate value which is fine amongst List which besides allows duplicates but Map doesn't allow duplicate key.
How to Convert Map into List inwards Java amongst Example
Map to List Example inwards Java
Here are few examples of converting Map to List inwards Java. We volition run into kickoff how to convert hashMap keys into ArrayList as well as thence hashMap values into ArrayList inwards Java.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
/**
*Converting HashMap into ArrayList inwards Java
*/
public flat MaptoListJava {
populace static void main(String... args) {
HashMap<String, String> personalLoanOffers = novel HashMap<String, String>();
// preparing hashmap amongst keys as well as values
personalLoanOffers.put("personal loan past times DBS", "Interest charge per unit of measurement low");
personalLoanOffers.put("personal loan past times Standard Charted", "Interest charge per unit of measurement low");
personalLoanOffers.put("HSBC personal loan past times DBS", "14%");
personalLoanOffers.put("Bankd of America Personal loan", "11%");
System.out.println("Size of personalLoanOffers Map: " + personalLoanOffers.size());
//Converting HashMap keys into ArrayList
List<String> keyList = novel ArrayList<String>(personalLoanOffers.keySet());
System.out.println("Size of Key listing from Map: " + keyList.size());
//Converting HashMap Values into ArrayList
List<String> valueList = novel ArrayList<String>(personalLoanOffers.values());
System.out.println("Size of Value listing from Map: " + valueList.size());
List<Entry> entryList = novel ArrayList<Entry>(personalLoanOffers.entrySet());
System.out.println("Size of Entry listing from Map: " + entryList.size());
}
}
Output:
Size of personalLoanOffers Map: 4
Size of Key listing from Map: 4
Size of Value listing from Map: 4
Size of Entry listing from Map: 4
That's all on Map to List Conversion inwards Java , equally y'all seen inwards higher upward instance y'all tin practice a split upward listing for both keySet as well as values Collection inwards Java. permit me know if y'all know whatever other agency except beast strength agency of going through each chemical ingredient of Map as well as copying into List.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Comparator as well as Comparable inwards Java
Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Comparator as well as Comparable inwards Java
0 Response to "How To Convert A Map To A Listing Inwards Coffee Example"
Post a Comment