【问题标题】:How to fetch the value of l1, l2, l3 out of this map?如何从这张地图中获取 l1、l2、l3 的值?
【发布时间】:2015-01-29 17:17:40
【问题描述】:

如何从这个map中获取list的值?

所有列表的值分开? 请尽快回复?

   public class Demo{
    public static void main(String args[]){

    List<String> l1=new ArrayList<String>();
    List<String> l2=new ArrayList<String>();
    List<String> l3=new ArrayList<String>();

    l1.add("1");
    l1.add("2");

    l2.add("a");
    l2.add("b");

    l3.add("1a");
    l1.add("2b");

    Map<String,List<String>> map=new HashMap<String,List<String>> ();
    map.put("List1",l1);
    map.put("List2",l2);
    map.put("List3",l3);
    }
    }

【问题讨论】:

  • map.get("List1") 会回复你l1...我不明白你的问题。

标签: java collections


【解决方案1】:

你可以这样做:

List<String> result = map.get("List1");

如果您不想存储它,也可以像这样遍历列表:

for(String x : map.get("List1")) {
    //do something
}

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2014-07-04
    • 2017-03-10
    • 2018-01-08
    • 2014-10-30
    • 2014-02-16
    • 1970-01-01
    • 2022-11-29
    • 2021-11-13
    相关资源
    最近更新 更多