Home>Article>Java> Basic usage and common APIs of Map collection system in Java

Basic usage and common APIs of Map collection system in Java

WBOY
WBOY forward
2023-01-21 07:30:02 1052browse

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.

Basic usage and common APIs of Map collection system in Java

Map collection overview and usage

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}

Basic usage and common APIs of Map collection system in Java

Characteristics of the Map collection system

Basic usage and common APIs of Map collection system in Java

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对象 Map maps = new HashMap(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 键一样会覆盖前面的 maps.put(null, null); // 键值对可以为null // 输出集合, 可以发现是无序的 System.out.println(maps); // {null=null, 凳子=10, 桌子=10}}
LinkedHashMap: The elements are

orderedaccording to the key, no duplication, no index, and no value requirements.

public static void main(String[] args) { // 创建一个LinkedHashMap对象 // Map maps = 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}}
TreeMap: The elements are sorted

according to the key, without duplication, without index, and the value is not required.

public static void main(String[] args) { // 创建一个HashMap对象 // Map maps = 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 collection commonly used API

Map collection:

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:

Method name Description put(K key,V value) Add element ##remove(Object key) clear() containsKey (Object key) containsValue(Object value) isEmpty() size()
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
put method adds elements

public static void main(String[] args) { // 创建Map集合对象 Map maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); System.out.println(maps); // {iPhone=6, 生活用品=15, 华为=10, 小米=5}}
remove method deletes elements based on keys

public static void main(String[] args) { // 创建Map集合对象 Map maps = 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}}
clear method, clears the collection elements

public static void main(String[] args) { // 创建Map集合对象 Map maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 清空元素 maps.clear(); System.out.println(maps); // {}}
containsKey() method, determines whether the specified key is included

public static void main(String[] args) { // 创建Map集合对象 Map maps = 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}
containsValue method, determines whether it contains the specified value

public static void main(String[] args) { // 创建Map集合对象 Map maps = 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}
isEmpty, determines whether the collection is empty

public static void main(String[] args) { // 创建Map集合对象 Map maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判断集合是否为空 System.out.println(maps.isEmpty()); // false}
size method, collection The number of elements

public static void main(String[] args) { // 创建Map集合对象 Map maps = new HashMap(); // 添加元素 maps.put("华为", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 返回集合元素的个数 System.out.println(maps.size()); // 4}
Extension method: putAll merges other collections. If the merge encounters duplicate keys, it will be merged

public static void main(String[] args) { Map 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}}
Recommended learning: "
java video tutorial

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete