Printing of Map collection k values and v values in Java

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


Preface

        Java是一门面向对象的编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程    

        Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点  。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等

        编写Java 代码的软件:

        Eclipse:一个开放源代码的、基于Java的可扩展开发平台 。

        NetBeans:开放源码的Java集成开发环境,适用于各种客户机和Web应用。

        IntelliJ IDEA:在代码自动提示、代码分析等方面的具有很好的功能。

        MyEclipse:由Genuitec公司开发的一款商业化软件,是应用比较广泛的Java应用程序集成开发环境。


提示:以下是本篇文章正文内容,下面案例可供参考

1. What is a collection?

        Simply put, a collection is a data container, which mainly includes Collection and Map collections.

Collections can only store objects, and each basic data type in Java has a corresponding reference type. For example, when storing an int type data in a collection, it must be automatically converted into the Integer class before storing it;
the collection stores a reference to the object, and the object itself is still stored in the heap memory;
the collection can store different types and unlimited quantities. data type.
  Features of the collection class: Provide a storage model with variable storage space, and the stored data capacity can change at any time

2. Usage steps

1. Question

        Today I’m going to catch up on my homework during the National Day holiday (one of the problems), which is to traverse, add, delete, and reassign values ​​in a collection.

        The question is as follows (example):

现在有一个map集合如下:
	Map<Integer,String> map = new HashMap<Integer, String>();
	        map.put(1, "张三丰");
	        map.put(2, "周芷若");
	        map.put(3, "汪峰");
	        map.put(4, "灭绝师太");
	要求:
	1)遍历集合,并将序号与对应人名打印。
	2)向该map集合中插入一个编码为5姓名为李晓红的信息
	3)移除该map中的编号为1的信息
	4)将map集合中编号为2的姓名信息修改为"周林"

2.Answer 

        Let’s talk about how to traverse the k value and v value of the set. The code is as follows:

        //1) Traverse the collection and print the serial number and the corresponding name
        for (int ke:map.keySet()){             String v=map.get(ke);             System.out.println(ke+"-----" +v);         }


map is the variable name of the set, and .keySet(); is the method to get the k value in the set, so that it will get the k value by itself. Use a loop to get the values ​​in the collection. In this way, the k value and v value can be looped out.

        The running results are as follows:

                        1-----Zhang Sanfeng
                        2-----Zhou Zhiruo
                        3-----Wang Feng
                        4-----Miejie Shitai

        Process ended with exit code 0

The code is as follows (example):

package com.xxgc.ch06.test;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class zy {
    public static void show(){
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(1, "张三丰");
        map.put(2, "周芷若");
        map.put(3, "汪峰");
        map.put(4, "灭绝师太");
        //1)遍历集合,并将序号与对应人名打印
        for (int ke:map.keySet()){
            String v=map.get(ke);
            System.out.println(ke+"-----"+v);
        }
        //2)向该map集合中插入一个编码为5姓名为李晓红的信息
        map.put(5,"李晓红");
        //3)移除该map中的编号为1的信息
        map.remove(1);
        //4)将map集合中编号为2的姓名信息修改为"周林"
        map.put(2,"周林");
        //
    }
    public static void main(String[] args) {
        show();
    }
}

The idea used here.


Summarize

Here is a summary of the article:
        The above is what we will talk about today. This article only briefly introduces the use of Java, and Java provides a large number of programming methods that allow us to quickly and easily.

.put(); is like adding data to a collection, and .remove(); is deleting. When deleting, it can be deleted based on the position or the content. Use a for loop to traverse the collection.

This ends this issue. Welcome to adopt.

Supongo que te gusta

Origin blog.csdn.net/qq_68384595/article/details/127198214
Recomendado
Clasificación