This article brings you relevant knowledge about java, which mainly introduces the basic use of Map collection system and related content of commonly used APIs. Let’s take a look at it together. I hope it will be helpful to everyone.
Map collection is a two-column collection, each element contains two data.
The format of each element of the Map collection: key=value (key-value pair element).
Map collection is also called "key-value pair collection".
Map collection overall format:
Collection collection format:
[Element 1, Element 2, Element 3..]
The full format of the Map collection:
{key1=value1, key2=value2, key3=value3, ...}
##Map One of the usage scenarios of the collection: Shopping cart system
Analysis:
The four items provided by the shopping cart and the purchased quantity are required in the background Container storage. Each product object corresponds to a purchase quantity one by one. Consider the product object as the creation of the Map collection, and the purchase quantity as the value of the Map collection. For example:{Item 1=2, Item 2=3, Item 3 = 2, Item 4= 3}
The most commonly used Map collection among the Map collections is HashMap. Focus on mastering HashMap, LinkedHashMap, and TreeMap. Other follow-up understanding.
Map collection system characteristics:
The characteristics of the Map collection are determined by the key. The keys of the Map collection are unordered, non-repeating, and non-indexed, and the values are not required (can be repeated). The values corresponding to the repeated keys at the end of the Map collection will overwrite the values of the previous repeated keys. The key-value pairs of the Map collection can be null.
Map collection implementation class features:
HashMap: The elements are unordered according to the key, no duplication, no index, and no requirements on the value. (Consistent with the Map system)public static void main(String[] args) { // 创建一个HashMap对象 MapLinkedHashMap: The elements aremaps = new HashMap(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 键一样会覆盖前面的 maps.put(null, null); // 键值对可以为null // 输出集合, 可以发现是无序的 System.out.println(maps); // {null=null, 凳子=10, 桌子=10}} orderedaccording to the key, no duplication, no index, and no value requirements.
public static void main(String[] args) { // 创建一个LinkedHashMap对象 // MapTreeMap: The elements are sortedmaps = new HashMap(); Map maps = new LinkedHashMap(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 键一样会覆盖前面的 maps.put(null, null); // 键值对可以为null // 输出集合, 是有序的 System.out.println(maps); // {桌子=10, 凳子=10, null=null}} according to the key, without duplication, without index, and the value is not required.
public static void main(String[] args) { // 创建一个HashMap对象 // Mapmaps = new HashMap(); // Map maps = new LinkedHashMap(); Map maps = new TreeMap(); // 向集合添加元素 maps.put("ddd", 2); maps.put("bbb", 10); maps.put("ddd", 3); maps.put("aaa", 5); maps.put("ccc", 1); // 输出集合, 元素按照键进行排序 System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3}}
Map is the ancestor interface of double-column collections, and its functions can be inherited and used by all double-column collections.
Map API is as follows:
Description | |
---|---|
Add element | |
According to the key, delete key-value pair elements | |
Remove all key-value pair elements | |
Determine whether the set contains the specified key | |
Determine whether the set contains the specified value | |
Judge whether the set is empty | |
The length of the set, that is The number of key-value pairs in the collection |
remove method deletes elements based on keyspublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10, 小米=5}}
clear method, clears the collection elementspublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 删除元素 maps.remove("小米"); System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10}}
containsKey() method, determines whether the specified key is includedpublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 清空元素 maps.clear(); System.out.println(maps); // {}}
containsValue method, determines whether it contains the specified valuepublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断是否包含指定键 System.out.println(maps.containsKey("华为")); // true System.out.println(maps.containsKey("魅族")); // false}
isEmpty, determines whether the collection is emptypublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断是否包含指定值 System.out.println(maps.containsValue(6)); // true System.out.println(maps.containsValue(99)); // false}
size method, collection The number of elementspublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断集合是否为空 System.out.println(maps.isEmpty()); // false}
Extension method: putAll merges other collections. If the merge encounters duplicate keys, it will be mergedpublic static void main(String[] args) { // 创建Map集合对象 Mapmaps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 返回集合元素的个数 System.out.println(maps.size()); // 4}
java video tutorialpublic static void main(String[] args) { MapRecommended learning: "map1 = new HashMap(); map1.put("java", 1); map1.put("C语言", 2); Map map2 = new HashMap(); map2.put("python", 4); map2.put("linux", 7); // 合并两个集合 map1.putAll(map2); System.out.println(map1); // {{python=4, java=7, C语言=2}}
The above is the detailed content of Basic usage and common APIs of Map collection system in Java. For more information, please follow other related articles on the PHP Chinese website!