Map的使用

 
package map;

import sun.plugin.security.Broken11ClassFixer;

import javax.swing.*;
import javax.xml.bind.SchemaOutputResolver;
import java.util.*;

/**
 * 
 * 
 * HashMap
 * 
 * key:唯一的    无序的
 * value:不唯一的   无序的
 * 
 * 
 * 
 * LinkedHashMap
 * key:  唯一的  有序(添加顺序)
 * value: 不唯一   无序的
 * 
 * 
 * 
 * 
 * TreeMap
 * key:唯一的     有序的(自然顺序)
 * value:不唯一的   无序的
 * 
 * 
 * 
 * Map的常用操作
 * 1.Map<String ,String > map = new HashMap<String,String>();
 * 2.map.put("Cn","中国");  //向map中添加数据,只能是key,value形式的
 * 3.map.get("us);  //得到map的value值
 * 4.遍历    Set<Map.Entry<String, String>> entries = map.entrySet();
 */
public class TestMapClass {
    public static void main(String[] args) {
        //创建一个Map存储对象
//        Map<String,String> map = new HashMap<String, String>();
//        Map<String,String> map = new LinkedHashMap<>();
        Map<String, String> map = new TreeMap<>();


        //向Map中添加数据
        map.put("CH", "中国");
        map.put("Us", "美国");
        map.put("Jp", "日本");
        map.put("En", "英国");
//        map.put(null, null);


        //输出Map对象数据
        System.out.println(map.toString());
        System.out.println(map.size());
        System.out.println(map.get("CH"));
        System.out.println(map.get("Jp"));
        System.out.println(map.keySet());
        System.out.println(map.values());


        /*
          遍历1 :  先得到所有的key,在遍历key的过程中,再根据key获取value  (不是很推荐使用这种遍历)
         */
        Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next + "========>" + map.get(next));

        }



        /*
          遍历2:得到所有的key-value组成的set,直接遍历set  (推荐使用)
         */
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entries1 : entries) {
            System.out.println(entries1.getKey() + "====>>" + entries1.getValue());

        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊昊浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值