集合之Map

Map:存放键值对,根据键对象找对应的值对象.键不能重复!
Map键不能重复,有唯一性,一般通过键找对应的的值
Map集合的特点:

1.具有映射关系
2.两列
3.一列要唯一 一列可以重复

键类似于 Set集合 无序,唯一
值类似于 List集合 有序 (在这里无效),可重复
值的顺序取决于键的顺序
键和值满足一定映射关系

添加功能

V put(K key, V value)
void putAll(Map<? extends K,? extends V> m)

删除功能

void clear()
V remove(Object key)

修改功能

V put(K key, V value)

遍历功能

V get(Object key)
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K,V>> entrySet()

判断功能

boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()

获取功能

V get(Object key)
int size()
Set<K> keySet()
Collection<V> values()

定义:

    Map<String,Integer> m1 = new HashMap<String, Integer>();

  添加或者修改

    m1.put("张三", 1213);
    m1.remove("张三");

  遍历1:
    for (String string : s) {
      String key = string;
      System.out.println(key+ " "+m1.get(key));
    }
    System.out.println(m1);

       遍历2:

    Set<Map.Entry<String, Integer>> set = m1.entrySet();
    for (Entry<String, Integer> entry : set) {

  entry.setValue(11111);
  String key = entry.getKey();
  int value = entry.getValue();
  System.out.println(key + " " + value);
}

原文地址:https://www.cnblogs.com/lrxvx/p/9407783.html