简体   繁体   English

关于 Java 的库映射类的问题?

[英]Questions about Java's library map classes?

The answers are (2) and (4) but not sure why.答案是(2)和(4)但不知道为什么。 I don't have much foundation on these topics.我对这些主题没有太多基础。 Could someone please explain why these are the correct answers and why the others are incorrect.有人可以解释为什么这些是正确的答案以及为什么其他答案是不正确的。 Thank you谢谢在此处输入图片说明

A HashMap is a data structure that consists of keys and values. HashMap 是一种由键和值组成的数据结构。 Values are stored in the HashMap with an associated key.值与关联的键一起存储在 HashMap 中。 The values can then be retrieved by recalling from the HashMap with the same key you used when putting the value in.然后可以通过使用放入值时使用的相同键从 HashMap 中调用来检索值。

1 1

TreeMaps and LinkedHashMaps are different versions of a Map. TreeMaps 和 LinkedHashMaps 是 Map 的不同版本。 A HashMap uses hashing to store its keys, whereas a TreeMap uses a binary search tree to store its keys and a LinkedHashMap uses a LinkedList to store keys. HashMap 使用散列来存储其键,而 TreeMap 使用二叉搜索树来存储其键,而 LinkedHashMap 使用 LinkedList 来存储键。 If you iterate over a HashMap, the keys will be returned in hash-sorted order (unpredictable in most cases), because that's how they were stored.如果您对 HashMap 进行迭代,则键将以哈希排序的顺序返回(在大多数情况下是不可预测的),因为它们就是这样存储的。 The TreeMap, however, has a tree of all the values, so when you iterate over the tree, you'll get all the keys in actual sorted order.然而,TreeMap 有一个包含所有值的树,因此当您遍历这棵树时,您将获得按实际排序顺序的所有键。 A LinkedHashMap has the keys in an ordered list, so an iterator will return the keys in the same order in which you inserted them. LinkedHashMap 具有有序列表中的键,因此迭代器将按照您插入键的相同顺序返回键。

2, 3, and 5 2、3 和 5

In a HashMap, values are looked up using their keys.在 HashMap 中,使用它们的键查找值。 If you had duplicate keys, the HashMap couldn't know which value to return.如果您有重复的键,则 HashMap 无法知道要返回哪个值。 Therefore, every key in a HashMap must be unique, but the values do not have to be.因此,HashMap 中的每个键都必须是唯一的,但值不必是唯一的。

4 4

In a normal HashMap, the key is hashed and then inserted in the appropriate spot.在普通的 HashMap 中,键被散列,然后插入到适当的位置。 With a TreeMap and a LinkedHashMap, you have the additional overhead of inserting the key into the tree or linked list which will take up additional time and memory.使用 TreeMap 和 LinkedHashMap,您有将键插入树或链表的额外开销,这将占用额外的时间和内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM