一个键(key)和它对应的值组成map鸠合中的一个元素。
Map中的元素是两个对象,一个对象作为键,一个对象作为值。键不能够反复,然则值能够反复。
Map自身是一个接口,要运用Map须要经由过程子类举行对象实例化。
在Map接口中有以下经常使用要领:
Map接口的经常使用子类有以下四个:HashMap、HashTable、TreeMap、ConcurrentHashMap。
如:
public class Test{ public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); map.put(1, "A"); map.put(1, "A+"); map.put(2, "B"); map.put(3, "C"); System.out.println(map); System.out.println(map.get(2)); //依据key获得value System.out.println(map.get(10)); //找不到返回null //获得Map中所有key信息 Set<Integer> set = map.keySet(); Iterator<Integer> iterator = set.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } }
注重:
(1)key值不许可反复,假如反复,则会把对应value值更新;
(2)key和value都许可为null,key为null有且只要一个。
HashMap道理:在数据量小的(JDK1.8后阈值为8)时刻,HashMap是根据链表的形式存储的;当数据量变大以后,为了举行疾速查找,会将这个链表变成红黑树(平衡二叉树)来举行保留,用hash来举行查找。
以上就是java中的map是什么的细致内容,更多请关注ki4网别的相干文章!