首页 > 编程 > Java > 正文

Java中的Map集合简单汇总解析

2019-11-26 08:29:24
字体:
来源:转载
供稿:网友

Map接口简介

Map接口是一种双列集合,它的每个元素都包含一个键对象Key和值对象Value,键和值对象之间存在一种对应关系,称为映射。从Map集合中访问元素时,只要指定了Key,就能找到对应的Value,

Map中的键必须是唯一的,不能重复,如果存储了相同的键,后存储的值会覆盖原有的值,简而言之就是键相同,值覆盖。

Map常用方法

put(K key, V value) 添加数据,如果先前包含该键的映射,则替换旧值  get(Object key)  返回指定键所映射的值  Set<Map.Entry<K,V>> entrySet() 返回地址中包含的映射的Set集合  Set<K> keySet() 返回集合中包含的键的Set集合  Collection<V> values() 返回集合中包含的值的Collection集合  remove(Object key) 从该集合中删除指定键的映射

HashMap集合

HashMap集合是Map接口的一个是实现类,它用于存储键值映射关系,但必须保证不出现重复的键。接下来通过一个案例来学习HashMap的用法

public class Example01 {  public static void main(String[] args) {      Map map = new HashMap();      map.put("1", "老大");      map.put("2", "老二");      map.put("3", "老三");      System.out.println("1"+" : "+map.get("1"));    //根据键获取值      System.out.println("2"+" : "+map.get("2"));      System.out.println("3"+" : "+map.get("3"));  }}

在Map中还提供了一个values()方法,通过这个方法可以直接获取Map中存储所有值的Collection集合。接下来通过一个案例来演示values()方法的使用

public class Example04 {  public static void main(String[] args) {    Map map = new HashMap();    map.put("1", "老大");    map.put("2", "老二");    map.put("3", "老三");    Collection values = map.values();    Iterator it = values.iterator();    while(it.hasNext()) {      Object value = it.next();      System.out.println(value);    }  }}

在程序开发中,经常需要取出Map中所有的键和值,那么如何遍历Map中所有的键和值呢?

第一种方法是先遍历Map集合中所有的键,再根据键获取相同的值

public class Example02 {  public static void main(String[] args) {    Map map = new HashMap();    map.put("1", "老大");    map.put("2", "老二");    map.put("3", "老三");    Set keySet = map.keySet();      //获取键的集合    Iterator it = keySet.iterator();      //迭代键的集合    while(it.hasNext()) {      Object key = it.next();      Object value = map.get(key);    //获取每个键所对应的值      System.out.println(key+" : "+value);    }  }}

第二种方法是先获取集合中的所有的映射关系,然后从映射关系中取出键和值。

public class Example03 {  public static void main(String[] args) {    Map map = new HashMap();    map.put("1", "老大");    map.put("2", "老二");    map.put("3", "老三");    Set set = map.entrySet();    Iterator it = set.iterator();        //获取迭代器对象    while(it.hasNext()) {      Map.Entry entry = (Map.Entry) it.next();        //获取集合中键值对映射      Object key = entry.getKey();      //获取Entry中的键      Object value = entry.getValue();      //获取Entry中的值      System.out.println(key+" : "+value);    }  }}

HashMap集合迭代出来元素的顺序和存入的顺序是不一致的。如果想让这两个顺序一致,可以使用Java中提供的LinkedHashMap类,它是HashMap的子类,与LinkedList一样,它也使用双向链表来维护内部元素的关系,使Map元素迭代的顺序与存入的顺序一致。

通过一个案例来学习LinkedHashMap的用法

public class Example05 {  public static void main(String[] args) {    Map map = new LinkedHashMap();      //创建Map集合    map.put("1", "老大");      //存储键和值    map.put("2", "老二");    map.put("3", "老三");    Set keySet = map.keySet();    Iterator it = keySet.iterator();    while(it.hasNext()) {      Object key = it.next();      Object value = map.get(key);      //获取每个键所对应的值      System.out.println(key+" : "+value);    }  }}

Properties集合

Map接口中还有一个实现类Hashtable,它和HashMap十份相似,区别在于Hashtable是线程安全的。Hashtable存取于元素时速度很慢,目前基本上被HashMap类所取代,但Hashtable类有一个子类Properties,在实际应用中非常重要

Properties只要用于存储字符串类型的键和值,在实际开发中,经常使用Properties集合来存取应用的配置项。假设有一个文本编辑工具,要求默认背景是红色,字体大小为14px,语言为中文,其位置如下

  Backgroup-color = red  Font-size = 14px  Language = chinese

在程序中可以使用Properties集合对这些配置进行存取,接下来通过一个案例来学习Properties集合的使用

public class Example06 {  public static void main(String[] args) {    Properties p = new Properties();    //创建Properties对象    p.setProperty("Backgroup-color", "red");    p.setProperty("Font-size ", "14px");    p.setProperty("Language", "chinese");    Enumeration names = p.propertyNames();    while(names.hasMoreElements()) {    //循环遍历所有的键      String key =(String) names.nextElement();      String value = p.getProperty(key);    //或获取键对应的值      System.out.println(key+" = "+value);    }  }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表