Home > Java > javaTutorial > body text

Basic usage and common APIs of Map collection system in Java

WBOY
Release: 2023-01-21 07:30:02
forward
1233 people have browsed it

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<string> 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}}</string>
Copy after login
LinkedHashMap: The elements are

ordered according to the key, no duplication, no index, and no value requirements.

public static void main(String[] args) {
    // 创建一个LinkedHashMap对象
    // Map<string> maps = new HashMap();
     Map<string> 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}}</string></string>
Copy after login
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<string> maps = new HashMap();
    // Map<string> maps = new LinkedHashMap();
    Map<string> 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}}</string></string></string>
Copy after login

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 nameDescriptionput(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<string> 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}}</string>
Copy after login
remove method deletes elements based on keys

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

public static void main(String[] args) {
    // 创建Map集合对象
    Map<string> maps = new HashMap();

    // 添加元素
    maps.put("华为", 10);
    maps.put("小米", 5);
    maps.put("iPhone", 6);
    maps.put("生活用品", 15);
  
    // 清空元素
    maps.clear();

    System.out.println(maps); // {}}</string>
Copy after login
containsKey() method, determines whether the specified key is included

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

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

public static void main(String[] args) {
    // 创建Map集合对象
    Map<string> maps = new HashMap();

    // 添加元素
    maps.put("华为", 10);
    maps.put("小米", 5);
    maps.put("iPhone", 6);
    maps.put("生活用品", 15);
  
    // 判断集合是否为空
    System.out.println(maps.isEmpty()); // false}</string>
Copy after login
size method, collection The number of elements

public static void main(String[] args) {
    // 创建Map集合对象
    Map<string> maps = new HashMap();

    // 添加元素
    maps.put("华为", 10);
    maps.put("小米", 5);
    maps.put("iPhone", 6);
    maps.put("生活用品", 15);
  
    // 返回集合元素的个数
    System.out.println(maps.size()); // 4}</string>
Copy after login
Extension method: putAll merges other collections. If the merge encounters duplicate keys, it will be merged

public static void main(String[] args) {
    Map<string> map1 = new HashMap();
    map1.put("java", 1);
    map1.put("C语言", 2);
    Map<string> map2 = new HashMap();
    map2.put("python", 4);
    map2.put("linux", 7);

    // 合并两个集合
    map1.putAll(map2);
    System.out.println(map1); // {{python=4, java=7, C语言=2}}</string></string>
Copy after login
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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template