Home > Java > javaTutorial > body text

Java Map collection example analysis

WBOY
Release: 2023-04-24 19:34:05
forward
840 people have browsed it

1. Preface

Map collection is a collection we often use. It is necessary to understand and use map collection

2. Map introduction

Basic form: public interface Map

Map is an interface. We cannot create objects directly. We can create objects in polymorphic form. There are two

parameters in Map, one is K representing the key. , one is V representing a value, and a key has and corresponds to a value. The Map cannot contain repeated

keys. If there are repeated keys added, the last key will prevail, and the other The key will be overwritten. The collections are all under the

java.util package, so the package needs to be imported.

There are generally two commonly used implementations, one is HashMap and the other is TreeMap

import java.util.HashMap;
import java.util.Map;
 
public class MapTest {
    public static void main(String[] args) {
        Map<String,String> map=new HashMap<>();
        map.put("2001", "张三");
        map.put("2002", "张三");
        map.put("2003", "李四");
      map.put("2003", "王五");//键重复,会覆盖上一个,留下最新的
        System.out.println(map);//{2003=王五, 2002=张三, 2001=张三}
    }
}
Copy after login

It can be seen from the above that the keys in the map cannot be repeated, but the values ​​​​can be repeated. Obtaining the contents of the

collection directly through the output object indicates that the toString method has been overridden in this collection.

3. Basic functions of Map

Java Map collection example analysis

These functions are common, just master them

Map function demonstration:

import java.util.HashMap;
 import java.util.Map;
 
public class MapTest {
    public static void main(String[] args) {
        //创建Map集合对象
        Map<String,String> map=new HashMap<>();
        //增加元素
        map.put("2001", "张三");
        map.put("2002", "李四");
        map.put("2003", "王五");
        System.out.println(map);//{2003=王五, 2002=李四, 2001=张三}
        //根据键删除元素
        //  map.remove("2001");
        //System.out.println(map);//{2003=王五, 2002=李四}
        //判断集合中是否包含指定的键返回boolean类型
        System.out.println(map.containsKey("2001"));//true
        System.out.println(map.containsKey("2004"));//false
        //判断集合中是否包含指定的值返回boolean类型
        System.out.println(map.containsValue("张三"));//true
        System.out.println(map.containsValue("赵六"));//false
        //判断集合是否为空返回boolean类型
        System.out.println(map.isEmpty());//false
        //得到集合的长度
        System.out.println(map.size());//3
        //清除所有键值对
        map.clear();
        System.out.println(map.isEmpty());//true,为空了
 
    }
}
Copy after login

4. Obtain function of Map collection

Java Map collection example analysis

#This method is mostly used when traversing collections. The first three are more commonly used and easier to remember.

Map traversal demonstration:

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 
public class Maptest2 {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("2001", "张三");
        map.put("2002", "李四");
        map.put("2003", "王五");
       //遍历集合
        //方式1:
        // 由键找值,创建键的集合
        Set<String>  keySet=map.keySet();
        //遍历键的集合,得到每一个键
        for (String key:keySet){
            //由键找值
            String value=map.get(key);
            //输出键和值
            System.out.print(key+" "+value+", ");
        }
        System.out.println("\n------------");
 
        //方式2:
        //获取所有键值对的集合
        Set<Map.Entry<String,String>>  entrySet  =map.entrySet();
        //遍历键值对集合
        for (Map.Entry<String,String> me:entrySet){
            //分别得到键和值
            String key=me.getKey();
            String value=me.getValue();
            System.out.print(key+" "+value+", ");
        }
    }
}
Copy after login

As shown:

Java Map collection example analysis

The above is the detailed content of Java Map collection example analysis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!