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();//返回集合中的键值对的对数 * */
Map集合的遍历
方式1,根据键查询值
获取所有键的集合
遍历键的集合,获取每一个键
根据键,查询值
方式2,根据键值对的对象查询键和值
获取所有键值对的对象的集合
遍历键值对的对象的集合,获取到每一个键值对的对象
根据键值对的对象,查询键和值
方式1,根据键查询值
/* * 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);
    }
  }
}
方式2,根据键值对的对象查询键和值
/* * 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
  }
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!