Map:双列集合的顶级接口,元素都是由key和value组成。Map的实现类有:HashMap、TreeMap和HashTable
HashMap
1.特点:
(1) key唯一,value可重复
(2)无序
(3)无索引
(4)线程不安全
(5)可以存null键,null值
2.数据结构:哈希表
3.方法:
V put(K key,V value) 添加元素,返回的是被覆盖的value
V reomove(Object key)根据key删除键值对,返回的是被删除的value
V get(Object key) 根据key获取value
boolean containsKey(Object key) 判断集合中是否包含指定的key
Collection<V> values() 获取集合中所有的value,转存到Collection集合中
HashMap的两种遍历方式
1. 获取key,再根据key获取value
Set<K> keySet() 将Map中的key获取出来,转存到Set集合中
public static void main(String[] args) { HashMap<Integer, String> map = new HashMap<>(); map.put(1,"世界"); map.put(2,"高山"); map.put(3,"江河"); Set<Integer> set = map.keySet(); for (Integer s : set) { System.out.println(s+","+map.get(s)); } }
2.同时获取key和value
a.获取记录key和value的对象:Map接口中的静态内部接口:Map.Entry
b.调用Map.Entry中的两个方法: getKey();getValue();
Set<Map.Entry<Integer, String>> entries = map.entrySet(); for (Map.Entry<Integer, String> entry : entries) { Integer key = entry.getKey(); String value = entry.getValue(); System.out.println(key+","+value); }
HashMap的去重是根据Key的HashCode进行去重的。如果Key是一个类属性,那么这个类需要对hashCode方法进行重写,否则属性值相同的对象会因为对象的地址不同而有不同的哈希值。
哈希表存储数据去重复的过程:
1.先比较元素的哈希值(重写hashCode),再比较内容(重写equals)
2.如果哈希值不一样,证明内容不一样,存
3.如果哈希值一样,再比较内容
a. 如果内容一样,那么去重复
b.如果内容不一样,存
细节:
1.哈希表中的数组默认长度为16,但是第一次put的时候数组才会被初始化长度为16的数组。
2.哈希表中有一个加载因子0.75F
含义:当数组存储达到75%时,扩容2倍;
3.如果链表长度达到8时,并且数组容量大于等于64的时候,链表就会自动转成红黑树
4.如果删除元素,元素个数小于等于6,红黑树就会转回链表。
LinkedHashMap extends HashMap
1.特点:
(1) key唯一,value可重复
(2)有序
(3)无索引
(4)线程不安全
(5)可以存null键,null值
2.数据结构:哈希表+双向链表
3.方法:和HashMap一样
V put(K key,V value) 添加元素,返回的是被覆盖的value
V reomove(Object key)根据key删除键值对,返回的是被删除的value
V get(Object key) 根据key获取value
boolean containsKey(Object key) 判断集合中是否包含指定的key
Collection<V> values() 获取集合中所有的value,转存到Collection集合中
HashTable
1.特点:
(1) key唯一,value可重复
(2)无序
(3)无索引
(4)线程安全
(5)不可以存null键,null值
2.数据结构:哈希表
3.HashMap和HashTable的区别:
(1)相同点:元素无序、无索引、key唯一
(2)不同点:HashMap线程不安全,HashTable线程安全。HashMap可以存储null键null值,而HashTable不可以。
Properities extends HashTable
1.特点:
(1) key唯一,value可重复
(2)无序
(3)无索引
(4)线程安全
(5)不可以存null键,null值
(6)key和value都是String型的,不需要指定类型
2.数据结构:哈希表
4.特有方法:
Object
setProperty(String key, String value)
调用 Hashtable 的方法put。
String
getProperty(String key)
用指定的键在此属性列表中搜索属性。
Set<String>
stringPropertyNames()
返回此属性列表中的键集,其中该键及其对应值是字符串,如果在主属性列表中未找到同名的键,则还包括默认属性列表中不同的键。
void
load(Reader reader)
按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty("username","root"); properties.setProperty("password","123456"); System.out.println(properties); System.out.println(properties.getProperty("username")); Set<String> strings = properties.stringPropertyNames(); System.out.println(strings); for (String s : strings) { System.out.println(properties.getProperty(s)); } }
TreeSet:是set的实现类
特点:
1.可对元素进行排序
2.元素唯一
3.无索引
4.线程不安全
5.底层数据结构:红黑树
TreeSet<String> set = new TreeSet<>(); set.add("a.nihao"); set.add("c.dajiaohao"); set.add("b.wohao"); System.out.println(set); TreeSet<Person> people = new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.getAge()-o2.getAge(); } }); people.add(new Person("留言",45)); people.add(new Person("xiaoqin",15)); people.add(new Person("xiaojing",16)); System.out.println(people);
TreeMap
1.特点:
(1) key唯一,value可重复
(2)可以对key进行排序
(3)无索引
(4)线程不安全
(5)不可以存null键,null值
2.数据结构:红黑树
TreeMap<String,String> map1 = new TreeMap<>(); map1.put("a","nihao"); map1.put("c","dajiaohao"); map1.put("b","wohao"); System.out.println(map1); TreeMap<Person,String> people1 = new TreeMap<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.getAge()-o2.getAge(); } }); people1.put(new Person("留言",45),"iiiiiii"); people1.put(new Person("xiaoqin",15),"5555555555"); people1.put(new Person("xiaojing",16),"77777"); System.out.println(people1);
补充:Vector集合
Vector集合是List接口的实现类。
特点:元素有序,有索引,元素可重复,线程安全
数据结构:数组
实现方法和ArrayList差不多。
源码分析:
1.如果用空参构造对象,数组初始容量为10.如果超出范围,那么自动扩容两倍
2.如果用有参构造创建对象,如果超出了范围,自动扩容,扩的是老数组长度+指定的容量增量。