zoukankan      html  css  js  c++  java
  • java 18


    需求:
      假设HashMap集合的元素是ArrayList。有3个。
      每一个ArrayList集合的值是字符串。
      元素如下,请遍历。
        结果:
          三国演义
              吕布
              赵云
          笑傲江湖
              令狐冲
              林平之
          神雕侠侣
              郭靖
              杨过

     1 package zl_MapDemo;
     2 
     3 import java.util.ArrayList;
     4 import java.util.HashMap;
     5 import java.util.Set;
     6 public class MapAndArrayList {
     7     
     8     public static void main(String[] args) {
     9         
    10             //创建HashMap集合
    11         HashMap<String , ArrayList<String>> hm = new HashMap<String,ArrayList<String>>();
    12             
    13             //创建集合元素1 三国演义   吕布   赵云
    14         ArrayList<String> a1 = new ArrayList<String>();
    15         
    16         //为集合1添加元素
    17         a1.add("吕布");
    18         a1.add("赵云");
    19         
    20         //把集合1添加到总集合中
    21         hm.put("三国演义", a1);
    22         
    23         //创建集合元素2 笑傲江湖    令狐冲     林平之
    24         ArrayList<String> a2 = new ArrayList<String>();
    25         
    26         //给集合2添加元素
    27         a2.add("令狐冲");
    28         a2.add("林平之");
    29         
    30         //把集合2添加到总集合中
    31         hm.put("笑傲江湖", a2);
    32         
    33         //创建集合元素3  神雕侠侣    郭靖    杨过  
    34         ArrayList<String> a3 = new ArrayList<String>();
    35         
    36         //给集合3添加元素
    37         a3.add("郭靖");
    38         a3.add("杨过");
    39         
    40         //把集合3添加到总集合中
    41         hm.put("神雕侠侣", a3);
    42         
    43         
    44         //进行总集合的遍历
    45         //获取总计和的键集合
    46         Set <String> HashSet = hm.keySet();
    47         
    48         //遍历
    49         for(String Hashkey : HashSet){
    50             
    51             //总计和的值是ArrayList<String>
    52             ArrayList<String> list = hm.get(Hashkey);
    53             
    54             //遍历ArrayList集合
    55             for(String s : list){
    56                 //获取值
    57                 System.out.println(s);
    58             }
    59             
    60         }        
    61         }        
    62 }
    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    HDU 2852 KiKi's K-Number (主席树)
    HDU 2089 不要62
    Light oj 1140 How Many Zeroes?
    Bless You Autocorrect!
    HDU 6201 transaction transaction transaction
    HDU1561 The more ,The better (树形背包Dp)
    CodeForces 607B zuma
    POJ 1651 Mulitiplication Puzzle
    CSUOJ 1952 合并石子
    Uva 1599 Ideal path
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5910036.html
Copyright © 2011-2022 走看看