HashMap集合嵌套ArrayList集合、ArrayList集合嵌套HashMap集合

HashMap集合嵌套ArrayList集合

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 * @program: javalearnCode
 * @description: HashMap集合嵌套ArrayList集合。
 * @author: chen
 * @create: 2018-09-03 11:34
 **/
public class HashMapArrayListDemo {
    public static void main(String[] args){

    /*
    假设HashMap有3个集合是ArrayList:
    三国演义
        诸葛亮
        周瑜
    红楼梦
        黛玉
        宝玉
    西游记
        悟空
        唐僧
     */

        //创建集合
        HashMap> novels= new HashMap>();

        ArrayList sanGuoCharacters= new ArrayList();
        sanGuoCharacters.add("诸葛亮");
        sanGuoCharacters.add("周瑜");

        ArrayList xiyoujiCharacters= new ArrayList();
        xiyoujiCharacters.add("悟空");
        xiyoujiCharacters.add("唐僧");

        ArrayList hongloumengCharacters= new ArrayList();
        hongloumengCharacters.add("黛玉");
        hongloumengCharacters.add("宝玉");

        novels.put("三国演义", sanGuoCharacters);
        novels.put("西游记", xiyoujiCharacters);
        novels.put("红楼梦", hongloumengCharacters);

        //遍历
        Set keys= novels.keySet();
        for(String key: keys){
            System.out.println(key);
            ArrayList characters=novels.get(key);
            //遍历集合,List特有的遍历方式size()和get()结合
            for(int i=0; i'\t'+character);
            }
//            增强for遍历集合
//            for(String character: characters){
//                System.out.println(character);
//            }

//            迭代器遍历集合
//            Iterator it= characters.iterator();
//            while(it.hasNext()){
//                String character= it.next();
//                System.out.println(character);
//            }


        }
    }
}

ArrayList集合嵌套HashMap集合

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 * @program: javalearnCode
 * @description: ArrayList集合嵌套HashMap
 * @author: chen
 * @create: 2018-09-03 16:29
 **/
public class ArraListHashMapDemo {
    public static void main(String[] args){
        //假设ArrayList有3个集合是HashMap
        /*
        吕布-貂蝉
        周瑜-小乔
        ---------------
        牛魔王-铁扇公主
        玉帝-王母
        ---------------
        贾琏-王熙凤
        贾宝玉-薛宝钗
        ---------------
         */
        //创建集合
        ArrayList> array= new ArrayList>();
        HashMap sanguoCouples= new HashMap();
        sanguoCouples.put("周瑜","小乔");
        sanguoCouples.put("吕布","貂蝉");

        HashMap xiyoujiCouples= new HashMap();
        xiyoujiCouples.put("牛魔王","铁扇公主");
        xiyoujiCouples.put("玉帝","王母");

        HashMap hongloumengCouples= new HashMap();
        hongloumengCouples.put("贾琏","王熙凤");
        hongloumengCouples.put("贾宝玉","薛宝钗");

        array.add(sanguoCouples);
        array.add(xiyoujiCouples);
        array.add(hongloumengCouples);

        //遍历集合
        //获取ArrayList集合中每个HashMap元素
        for(HashMap couples: array){
            //先获取所有键
            Set husbands=couples.keySet();
            for(String husband: husbands){
                //根据键获取值
                String wife= couples.get(husband);
                System.out.println(husband+"-"+wife);
            }

            System.out.println("---------------");
        }
    }
}

你可能感兴趣的:(java)