How to Store HashMap<String, ArrayList> Inside a List – 如何在列表中存储HashMap<String, ArrayList>?

最后修改: 2022年 4月 20日

1. Overview


In this tutorial, we’re going to discuss how to store a HashMap inside a List in Java. First, we’ll have a short explanation of HashMap and List data structures in Java. Then, we’ll write a simple code to solve the problem.


2. HashMap and List in Java


Java provides us with different data structures with various properties and characteristics to store objects. Among those, HashMap is a collection of key-value pairs that maps a unique key to a value. Also, a List holds a sequence of objects of the same type.

Java为我们提供了具有各种属性和特征的不同数据结构来存储对象。其中,HashMap是一个键值对的集合,它将一个唯一的键映射到一个值。另外,a List持有相同类型的对象的序列。

We can put either simple values or complex objects in these data structures.


3. Store HashMap<String, ArrayList<String>> Inside a List

3.在List内存储HashMap<String, ArrayList<String>>

Let’s have a simple example in which we create a List of HashMaps. For each book category, there is a HashMap that maps the name of a book to its authors.

让我们举个简单的例子,我们创建一个ListHashMaps。 对于每个图书类别,都有一个HashMap,将图书的名称映射到其作者。

First, we define javaBookAuthorsMap, which maps the name of a Java-related book to its list of authors:


HashMap<String, List<String>> javaBooksAuthorsMap = new HashMap<>();

Also, we define phpBooksAuthorsMap to hold the name and authors of a book for the PHP category:


HashMap<String, List<String>> phpBooksAuthorsMap = new HashMap<>();

Then, we define booksAuthorsMapsList to hold HashMaps for different categories:


List<HashMap<String, List<String>>> booksAuthorsMapsList = new ArrayList<>();

Now, we have a List containing two HashMaps.


To test it, we can put some books information in javaBookAuthorsMap and phpBooksAuthorsMap lists. Then, we add them to the booksAuthorsMapsList. Finally, we make sure that the HashMaps are added to the List.

为了测试它,我们可以将一些书籍信息放入 javaBookAuthorsMap phpBooksAuthorsMap lists.然后,我们将它们添加到booksAuthorsMapsList.最后,我们确保HashMaps被添加到List.

Let’s see the unit test below:


public void givenMaps_whenAddToList_thenListContainsMaps() {
    HashMap<String, List<String>> javaBooksAuthorsMap = new HashMap<>();
    HashMap<String, List<String>> phpBooksAuthorsMap = new HashMap<>();
    javaBooksAuthorsMap.put("Head First Java", Arrays.asList("Kathy Sierra", "Bert Bates"));
    javaBooksAuthorsMap.put("Effective Java", Arrays.asList("Joshua Bloch"));
    javaBooksAuthorsMap.put("OCA Java SE 8",
      Arrays.asList("Kathy Sierra", "Bert Bates", "Elisabeth Robson"));
    phpBooksAuthorsMap.put("The Joy of PHP", Arrays.asList("Alan Forbes"));
    phpBooksAuthorsMap.put("Head First PHP & MySQL",
    Arrays.asList("Lynn Beighley", "Michael Morrison"));



4. Conclusion


In this article, we talked about storing HashMaps inside a List in Java. Then, we wrote a simple example in which we added HashMap<String, ArrayList<String>> to a List<String> for Two book categories.

在这篇文章中,我们谈到了在Java中把HashMaps存储在List中。然后,我们写了一个简单的例子,在其中我们为两个图书类别的HashMap<String, ArrayList<String>>/em>添加了一个List<String>

The examples are available over on GitHub.