【问题标题】:How can I store HashMap<String, ArrayList<String>> inside a list?如何将 HashMap<String, ArrayList<String>> 存储在列表中?
【发布时间】:2013-05-07 03:15:54
【问题描述】:

我的 hashmap 将字符串存储为键,将数组列表存储为值。现在,我需要将其嵌入到列表中。也就是说,它将具有以下形式:

List<HashMap<String, ArrayList<String>>>

这些是我使用的声明:

Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
ArrayList<String> arraylist = new ArrayList<String>();
map.put(key,arraylist);
List<String> list = new ArrayList<String>();

谁能帮我在列表中使用哪种方法以及如何继续将我的地图存储到其中?

【问题讨论】:

  • 你的意思是你需要一个这种形式的地图列表?
  • 我假设您使用的是 java 并且您的大部分 ( 实际上应该是 &lt;
  • 是的..它的java..由于'
  • 是的......存储我的地图详细信息的列表......并且检索我的列表应该显示我的地图整体......

标签: java list hashmap append


【解决方案1】:

尝试以下方法:

List<Map<String, ArrayList<String>>> mapList = 
    new ArrayList<Map<String, ArrayList<String>>>();
mapList.add(map);

如果您的列表必须是List&lt;HashMap&lt;String, ArrayList&lt;String&gt;&gt;&gt; 类型,则将您的map 变量声明为HashMap 而不是Map

【讨论】:

    【解决方案2】:

    首先您需要将List 定义为:

    List<Map<String, ArrayList<String>>> list = new ArrayList<>();
    

    要将Map 添加到List,请使用add(E e) 方法:

    list.add(map);
    

    【讨论】:

    • 鉴于maplist 的当前定义,这将不起作用。
    • 我回滚了对这篇文章的编辑,因为编辑没有发表评论或作者没有承认。
    【解决方案3】:

    首先,让我稍微修正一下你的声明:

    List<Map<String, List<String>>> listOfMapOfList = 
        new HashList<Map<String, List<String>>>();
    

    请注意我只使用了一次具体类 (HashMap)。在以后可以更改实现的地方使用接口很重要。

    现在您想将元素添加到列表中,不是吗?但是元素是一个地图,所以你必须创建它:

    Map<String, List<String>> mapOfList = new HashMap<String, List<String>>();
    

    现在您要填充地图。幸运的是,您可以使用为您创建列表的实用程序,否则您必须单独创建列表:

    mapOfList.put("mykey", Arrays.asList("one", "two", "three"));
    

    好的,现在我们准备将地图添加到列表中:

    listOfMapOfList.add(mapOfList);
    

    但是:

    立即停止创建复杂的集合!想想未来:您可能不得不将内部映射更改为其他内容或列表设置等。这可能会导致您重新编写代码的重要部分。而是定义包含您的数据的类,然后将其添加到一维集合中:

    让我们给你的班级打电话Student(作为例子):

    public Student {
        private String firstName;
        private String lastName;
        private int studentId;
    
        private Colectiuon<String> courseworks = Collections.emtpyList();
    
        //constructors, getters, setters etc
    }
    

    现在你可以定义简单的集合了:

    Collection<Student> students = new ArrayList<Student>();
    

    如果将来您想将您的学生放到地图中,其中关键是 studentId,请执行以下操作:

    Map<Integer, Student> students = new HashMap<Integer, Student>();
    

    【讨论】:

    • 很好的答案,希望你不介意我调整了你的代码格式。
    【解决方案4】:

    始终尝试在 Collection 中使用接口引用,这增加了更多灵活性。
    下面的代码有什么问题?

    List<Map<String,List<String>>> list = new ArrayList<Map<String,List<String>>>();//This is the final list you need
    Map<String, List<String>> map1 = new HashMap<String, List<String>>();//This is one instance of the  map you want to store in the above list.
    List<String> arraylist1 = new ArrayList<String>();
    arraylist1.add("Text1");//And so on..
    map1.put("key1",arraylist1);
    //And so on...
    list.add(map1);//In this way you can add.
    

    你可以像上面那样轻松地做到这一点。

    【讨论】:

      【解决方案5】:
      class Student{
          //instance variable or data members.
      
          Map<Integer, List<Object>> mapp = new HashMap<Integer, List<Object>>();
          Scanner s1 = new Scanner(System.in);
          String name = s1.nextLine();
          int regno ;
          int mark1;
          int mark2;
          int total;
          List<Object> list = new ArrayList<Object>();
          mapp.put(regno,list); //what wrong in this part?
          list.add(mark1);
          list.add(mark2);**
          //String mark2=mapp.get(regno)[2];
      }
      

      【讨论】:

      • 对这段代码的一些解释可能会有所帮助。
      • 是的,当然。事实是我需要解决上述问题的方法,您能否将其清除。在下一篇文章中,我将正确解释整个代码。
      猜你喜欢
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 2023-04-10
      • 2016-05-07
      • 1970-01-01
      相关资源
      最近更新 更多