How To Convert A Map To A Listing Inwards Coffee Example

Advertisement

Masukkan script iklan 970x90px

How To Convert A Map To A Listing Inwards Coffee Example

Sabtu, 18 April 2020

Map together with List are 2 mutual information structures available inwards Java together with inwards this article nosotros volition meet how tin flame nosotros convert Map values or Map keys into List inwards Java. The primary departure betwixt Map (HashMap, ConcurrentHashMap or TreeMap) together with List is that Map holds 2 objects fundamental together with value spell List but holds ane object which itself is a value. Key inwards hashmap is but an addon to honour values, hence you lot tin flame but alternative the values from Map together with usage a List out of it. The map inwards Java allows duplicate value which is fine amongst List which likewise 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 meet get-go how to convert hashMap keys into ArrayList together with and then 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 degree MaptoListJava {

    populace static void main(String... args) {
        HashMap<String, String> personalLoanOffers = novel HashMap<String, String>();
        // preparing hashmap amongst keys together with values
        personalLoanOffers.put("personal loan yesteryear DBS", "Interest charge per unit of measurement low");
        personalLoanOffers.put("personal loan yesteryear Standard Charted", "Interest charge per unit of measurement low");
        personalLoanOffers.put("HSBC personal loan yesteryear 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

Map together with List are 2 mutual information structures available inwards Java together with inwards this article nosotros volition  How to Convert a Map to a List inwards Java Example
That's all on Map to List Conversion inwards Java , every bit you lot seen inwards higher upwards instance you lot tin flame usage a dissever listing for both keySet together with values Collection inwards Java. allow me know if you lot know whatever other means except beast forcefulness means of going through each chemical constituent of Map together with copying into List.

Further Learning
Java In-Depth: Become a Complete Java Engineer
Difference betwixt Comparator together with Comparable inwards Java