Home  >  Article  >  Java  >  Detailed explanation and example code of java HashMap

Detailed explanation and example code of java HashMap

高洛峰
高洛峰Original
2017-02-03 17:28:591632browse

java HashMap

/*
* Map集合的特点
* 将键映射值的对象,一个映射不能包含重复的值;每个键最多只能映射到一个值
* 
* Map集合和Collection集合的区别?
* Map集合存储元素是成对出现的,Map集合的键是唯一的,就是可重复的。可以把这个理解为:夫妻对
* Collection集合存储元素是单独出现的,Collection的儿子Set是唯一的,List是可重复的,可以把这个理解为:光棍
* 
* 注意:
* Map集合的数据结构值针对键有效,限值无效
* Collection集合的数据结构是针对元素有效
* 
* Map集合的功能概述:
* 1:添加功能
* V put(K key,V value);//添加元素
* 如果键是第一次存储,就直接存储元素,返回null
* 如果键不是第一次存储,就用值把以前的值替换掉,返回以前的值
* 
* 2:删除功能
* void clear();//移除所有的键值对元素
* V remove(Object key);//根据键删除键值对元素,并把值返回
* 
* 3:判断功能
* boolean containsKey(Object key);//判断集合是否包含指定的键
* boolean containsValue(Object value);//判断集合是否包含指定的值
* boolean isEmpty();//判断集合是否为空
* 
* 4:获取功能
* set<Map,Entry<E,V>> entrySet();获取键值对的对象集合
* V get(Object key);//根据键获取值
* Set<K> keySet();//获取集合中所有键的集合
* Collection<V> values();//获取集合中所有值的集合
* 
* 5:长度功能
* int size();//返回集合中的键值对的对数
* */

Traversal of Map collection

Method 1, query value based on key

Get the collection of all keys

Traverse the collection of keys , get each key

Query the value based on the key

Method 2, query the key and value based on the key-value pair object

Get the collection of all key-value pair objects

Traverse the collection of key-value pair objects and obtain the object of each key-value pair

Query the key and value based on the key-value pair object

Method 1, Query the value based on the key

/*
 
* Map集合的遍历,根据键查询值
* 
* 思路:
* A:获取所有的键
* B:遍历键的集合,获取得到每一个键
* C:根据键查询值
* */
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
  
/*
 * Map集合的遍历,根据键查询值
 *
 * 思路:
 * A:获取所有的键
 * B:遍历键的集合,获取得到每一个键
 * C:根据键查询值
 * */
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    Map<String, String> map = new HashMap<String, String>();
  
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
  
    System.out.println(map);
  
    // A:获取所有的键
    Set<String> set = map.keySet();
  
    // B:遍历键的集合,获取得到每一个键
    for (String key : set) {
      // C:根据键查询值
      String value = map.get(key);
      System.out.println(key + "---" + value);
    }
  }
}

Method 2, query the key and value based on the object of the key-value pair

/*
* Map集合的遍历,根据对象查询键和值
*
* 思路:
* A:获取所有的键值对对象的集合
* B:遍历键值对对象的集合,得到每一个键值对的对象
* C:获取键和值
* */
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
  
/*
 * Map集合的遍历,根据对象查询键和值
 *
 * 思路:
 * A:获取所有的键值对对象的集合
 * B:遍历键值对对象的集合,得到每一个键值对的对象
 * C:获取键和值
 * */
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    Map<String, String> map = new HashMap<String, String>();
  
    map.put("hello", "world");
    map.put("java", "c++");
    map.put("sql", "os");
  
    System.out.println(map);
  
    // A:获取所有的键值对对象的集合
    Set<Map.Entry<String, String>> set = map.entrySet();
  
    // B:遍历键值对对象的集合,得到每一个键值对的对象
    for (Map.Entry<String, String> me : set) {
      // C:获取键和值
      String key = me.getKey();
      String value = me.getValue();
      System.out.println(key + "---" + value);
    }
  }
}

/*
* 1:HashMap和Hashtable的区别?
* HashMap线程不安全,效率高,允许null键和null值
* Hashtable线程安全,效率低,不允许null键和null值
*
* 2:List,Set,Map等接口是否都继承于Map接口?
* List,Set不是继承自Map接口,它们继承自Collection接口
* Map接口本身就是一个顶层接口
* */
import java.util.HashMap;
import java.util.Hashtable;
  
public class IntegerDemo {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
  
    HashMap<String, String> hm = new HashMap<String, String>();
    Hashtable<String, String> ht = new Hashtable<String, String>();
  
    hm.put("hello", "world");
    hm.put("java", "c++");
    hm.put(null, "sql");
  
    ht.put("hello", "world");
    ht.put("java", "c++");
    ht.put(null, "sql");// Exception in thread "main"
              // java.lang.NullPointerException
  }
}

Thanks for reading, I hope this helps everyone, and thank you for your support of this site!

For more java HashMap detailed explanations and example code related articles, please pay attention to the PHP Chinese website!

Statement:
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