map 使用哪种数据结构?2024 年 8 月 28 日 | 阅读 9 分钟 map 数据类型表示一个无序的键值对项集合。将 map 数据类型分配给端口,以便通过转换传递 map 数据。map 元素是一个键和一个值对,对应一个对象并将其映射到另一个对象。Spark 引擎可以从复杂文件中读取和写入 map 数据,也可以通过映射传递 map 数据。 键值对类似于在字典中查找一个词。键是单词,值是该词的含义或定义。类似地,在计算中,键值对关联两个数据位,其中一部分,“键”,可以用来查找另一部分,“值”。map 数组数据类型是一种 ADT(抽象数据类型),它在数组中存储键值对(k-v)。“键”是数据标识符,而“值”是被识别或存储的材料。你学校的每个储物柜都有不同的钥匙或不同的密码锁。如果你要列出每个储物柜的编号和钥匙,那么“键”就是储物柜编号,“值”就是对应每个储物柜的密码锁或钥匙编号。当考虑到美国所有的州及其首府时,“键”就是州名,而首府就是“值”。 map 数据类型有几个显著的特点。因为键是独一无二的,所以不可能有重复的键。例如,如果你需要列出美国所有城镇的名称和人口,就不能使用 map 数据类型,因为城镇名称会重复。华盛顿州、新泽西州和威斯康星州都有名为“Washington”的城镇。 当数据已经记录到 map 数据类型中时,可以通过三种方式查看:
map 数据类型在 Java 中由几个 Java 类实现,包括 HashMap、HashTable 和 TreeMap。在 HashMap 中,键和值是无序的。在 TreeMap 中,值是按键排序的。HashTable 是同步的,并且不允许空的键或值。 map 数据类型被称为关联数组,因为它像数组一样是值的集合,而不是像 Int 或 String 那样的单个值。此外,每个唯一的键都与一个值相关联,从而形成一个关联数组。 Java 代码输出 The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Nirnay Age of Student acting as Map of Map:: 23 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Alex Age of Student acting as Map of Map:: 26 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Paul Age of Student acting as Map of Map:: 20 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Merina Age of Student acting as Map of Map:: 32 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Gwen Age of Student acting as Map of Map:: 22 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Tom Age of Student acting as Map of Map:: 61 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 2 Map:: Name : Alex -> Key : 26 Name : Tom -> Key : 61 Name : Nirnay -> Key : 23 Name : Merina -> Key : 32 Name : Gwen -> Key : 22 Name : Paul -> Key : 20 Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 3 Name(key) of Student whose age(value) want to get:: Nirnay Age(value) is:: 23 Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 4 The size of the map is:: 6 Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 5 Name(key) of Student:: Alex Name(key) Alex is present :: true Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 2 Map:: Name : Alex -> Key : 26 Name : Tom -> Key : 61 Name : Nirnay -> Key : 23 Name : Merina -> Key : 32 Name : Gwen -> Key : 22 Name : Paul -> Key : 20 Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 1 Name of Student acting as Key of Map:: Nick Age of Student acting as Map of Map:: 18 Object added to the Map. Wanna proceed further(Type y or n)? y The options list for Map:: 1. To add an object in the Map 2. To print all objects in the Map 3. To get the Value(age) of a certain Key(student) 4. To print the size of Map 5. To verify does a key(student) exist in the Map. 2 Map:: Name : Alex -> Key : 26 Name : Tom -> Key : 61 Name : Nick -> Key : 18 Name : Nirnay -> Key : 23 Name : Merina -> Key : 32 Name : Gwen -> Key : 22 Name : Paul -> Key : 20 Wanna proceed further(Type y or n)? n 在上面的代码中,我们借助不同的函数成功地实现了 Map 数据结构,这些函数用于执行 map 数据结构的不同功能。在上面编写的代码中,我们编写了一个用于在 map 数据类型中添加新数据的函数,以及另一个用于显示 map 数据类型中所有现有数据的函数。还有一个函数,用于获取存储在 map 数据类型中与特定键关联的值。还编写了一个返回 map 数据类型总大小的函数。最后,还编写了另一个函数,用于检查现有 map 数据类型中是否存在某个特定的键。最终,程序末尾编写了一个菜单驱动的程序,为此创建了一个 hashmap 的对象,用于按菜单驱动的顺序调用所有这些上面编写的函数。 因此,通过本文,我们了解了什么是 map 数据类型,以及如何在 Java 编程语言中实现一个简单的 map 数据类型。我们还看到了如何通过调用 hashmap 类提供的各种内置函数,在 Java 编程语言中使用 map 数据类型的不同功能。 在 Java 编程语言中,map 本身是一个接口。在我们的示例中,我们使用了 hashmap,它是一个扩展此特定接口的类。除了 hashmap 类,Java 中还有许多类扩展了 map 接口。在 Java 编程语言中扩展 map 接口的类的一些例子有 HashMap、TreeMap 和 LinkedHashMap。 下一主题什么是数据结构中的迭代 |
我们请求您订阅我们的新闻通讯以获取最新更新。