1. Overview
1.概述
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.
在本教程中,我们将讨论如何在Java中的HashMap内存储一个List。首先,我们将对Java中的HashMap和List数据结构进行简短的解释。然后,我们将写一段简单的代码来解决这个问题。
2. HashMap and List in Java
2.Java中的HashMap和List
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.
让我们举个简单的例子,我们创建一个List的HashMaps。 对于每个图书类别,都有一个HashMap,将图书的名称映射到其作者。
First, we define javaBookAuthorsMap, which maps the name of a Java-related book to its list of authors:
首先,我们定义了javaBookAuthorsMap,,它将与Java有关的书的名字映射到它的作者列表。
HashMap<String, List<String>> javaBooksAuthorsMap = new HashMap<>();
Also, we define phpBooksAuthorsMap to hold the name and authors of a book for the PHP category:
同时,我们定义了phpBooksAuthorsMap来保存PHP类别的书籍名称和作者。
HashMap<String, List<String>> phpBooksAuthorsMap = new HashMap<>();
Then, we define booksAuthorsMapsList to hold HashMaps for different categories:
然后,我们定义booksAuthorsMapsList来保存不同类别的HashMaps。
List<HashMap<String, List<String>>> booksAuthorsMapsList = new ArrayList<>();
Now, we have a List containing two HashMaps.
现在,我们有一个List包含两个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:
让我们看看下面的单元测试。
@Test
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"));
booksAuthorsMapsList.add(javaBooksAuthorsMap);
booksAuthorsMapsList.add(phpBooksAuthorsMap);
assertTrue(booksAuthorsMapsList.get(0).keySet()
.containsAll(javaBooksAuthorsMap.keySet().stream().collect(Collectors.toList())));
assertTrue(booksAuthorsMapsList.get(1).keySet()
.containsAll(phpBooksAuthorsMap.keySet().stream().collect(Collectors.toList())));
}
4. Conclusion
4.总结
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.
这些例子可以在GitHub上找到over。