Convert key and values in Map to List, mapkeyvalueslist

Source: Internet
Author: User
Tags addall

Convert key and values in Map to List, mapkeyvalueslist

In project development, Map is often used, and the storage of map is unordered. It stores key-value pairs, that is, a key corresponds to a value. Sometimes the key and value of Map need to be converted to List for related operations. Now, the key and value of Map can be converted through instances.

The Code is as follows:

Package com. example; import java. util. arrayList; import java. util. hashMap; import java. util. hashSet; import java. util. iterator; import java. util. list; import java. util. map; import java. util. set; public class JavaTest {public static void main (String [] args) {Map <String, String> myMap = new HashMap <String, String> (); myMap. put ("a", "Zhang San"); myMap. put ("B", "Li Si"); myMap. put ("c", "Wang Wu"); myMap. put ("d", ""); Set <String> set = getKeySetByMap (myMap); List <String> keyListBySet = getKeyListBySet (set ); list <String> keyList = getListByMap (myMap, true); List <String> valuesList = getListByMap (myMap, false); Iterator <String> it = set. iterator (); while (it. hasNext () {String key = it. next (). toString (); System. out. println ("set:" + key) ;}for (int I = 0; I <keyListBySet. size (); I ++) {System. out. println ("keyListBySet:" + I + "->" + keyListBySet. get (I) + "; key:" + keyList. get (I) + "; values:" + valuesList. get (I) ;}}/*** get the key contained in the map according to the map, return set ** @ param map * @ return */public static Set <String> getKeySetByMap (Map <String, String> map) {Set <String> keySet = new HashSet <String> (); keySet. addAll (map. keySet (); return keySet ;} /*** return the list of keys based on the set of keys ** @ param set * @ return */public static List <String> getKeyListBySet (Set <String> set) {List <String> keyList = new ArrayList <String> (); keyList. addAll (set); return keyList;}/*** the list of keys and values returned by map ** @ param map * @ param isKey * true is the key, false: value * @ return */public static List <String> getListByMap (Map <String, String> map, boolean isKey) {List <String> list = new ArrayList <String> (); Iterator <String> it = map. keySet (). iterator (); while (it. hasNext () {String key = it. next (). toString (); if (isKey) {list. add (key);} else {list. add (map. get (key) ;}} return list ;}}

The output result is as follows:

Set: dset: bset: cset: akeyListBySet: 0-> d; key: d; values: John keyListBySet: 1-> B; key: B; values: Li Si keyListBySet: 2-> c; key: c; values: Wang Wu keyListBySet: 3-> a; key: a; values: Zhang San




How does the key value of Map <List, List> correspond?

If the key in the map is list, it is equivalent to the list address as the key. Only the address of the entire list can be called up. It cannot match the elements in the next list one by one. This should be the case.
Map <String, WzMaterialsBigcategory> map = new HashMap <String, WzMaterialsBigcategory> ();

For (WzMaterialsBigcategory wz: list ){
Map. put (wz. getCategoryid, wz );

}
// Call map. get (key) to obtain the corresponding object.

How does java retrieve the value of regionId in the returned map and the value (regionId) is also of the ArrayList type?

What does the landlord mean? When using this map, do you know the key?
If you know, you can just get OK soon.
List list = map. get ("regionId ")
For (int I = 0; I <list. size (); I ++)
{
List. get (I). toString ();
}

If you do not know, first
Output the value of map. keyset () cyclically. Let's see what is there and then proceed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.