zoukankan      html  css  js  c++  java
  • java map接口

    Map提供了一种映射关系,其中的元素是以键值对(key-value)的形式存储的,能够实现根据key快速查找value;

    1.Map中的键值对以Entry类型的对象实例形式存在;

    2.建(key值)不可重复,value值可以重复,一个value值可以和很多key值形成对应关系,每个建最多只能映射到一个值。

    3.Map支持泛型,形式如:Map<K,V>

    4.map 接口和c# 中的Dictionary相近

    一、基本用法

    Map<String,String>map=new HashMap<String, String>();
    map.put("key", "value");//添加
    map.get("key") ;//获取
    map.clear();//清空
    map.remove("key");//返回value值
    map.containsKey("key");//是否包含key值

    二、迭代用法

       keySet迭代方法:

    Map<String, String> map = new HashMap<String, String>();
            map.put("key", "value");// 添加
            Set<String> keys = map.keySet();
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                String s = it.next();
                System.out.println(s);
            }

       values迭代方法:

    Map<String, String> map = new HashMap<String, String>();
            map.put("key", "value");// 添加
            Collection<String>values= map.values();
            Iterator<String> it = values.iterator();
            while (it.hasNext()) {
                String s = it.next();
                System.out.println(s);
            }

      Entry迭代方法:

    Map<String, String> map = new HashMap<String, String>();
            map.put("key", "value");// 添加
            Set<Map.Entry<String, String>> set = map.entrySet();
            Iterator<Map.Entry<String, String>> it = set.iterator();
            while (it.hasNext()) {
               System.out.println(it.next()); 
            }
    结果:key=value
  • 相关阅读:
    核心动画的使用
    核心动画的使用
    核心动画的使用
    异常上报功能Bugly简介
    核心动画的使用
    一种传统的程序模块化马甲包开发架构
    定时器、多线程
    核心动画
    Core Animation 核心动画
    核心动画的使用
  • 原文地址:https://www.cnblogs.com/wswbk/p/6777647.html
Copyright © 2011-2022 走看看