Map接口
Map接口概述
与Set接口和List接口不同,Map接口并未继承Collection接口。 Map接口存储双列数据,Map<key,value>即key-value对的数据。
Map的结构
Map中的key:无序的,不可重复的 使用Set存储所有key -->要求所在的类重写equals()和HashCode() (以HashMap()为例)
Map中的value:无序的,可重复的 使用Collection存储 -->要求所在的类重写equals()
一个键值key-value构成一个Entry对象
Map中的Entry:无序的,不可重复的,使用Set存储所有的Entry
Map接口的实现类
HashMap:作为Map的主要实现类,线程不安全,效率高,可以存储null的key和value。底层:数组+链表+红黑树。
LinkedHashMap:HashMap的子类。保证遍历map元素时,可以按照添加的顺序实现遍历。 原因:在原因的Map基础上添加了元素的前驱和后继。适合频繁的遍历操作。
TreeMap:保证按照添加的key-value对进行排序,实现排序遍历。此时,考虑key的自然排序和定制排序。底层;红黑树(最小推)
Hashtable:作为Map的古老实现类,线程安全,效率低,不能存储null的key和value
Properties:Hashtable类的子类。常用来处理配置文件,key-value都是String类型
Map接口的常用方法——以HashMap为例
Map的常用方法:
添加:Object put(OBject Key,Object value) , void putAll(Map m)
删除:Object remove(Object key), boolean remove(Object key,Object value)
修改:Object put(OBject Key,Object value)
查询:Object get(Object key) , boolean equals(Object o)
长度:size()
遍历:entrySet() values() keySet()
public static void Test01(){
HashMap hashMap = new HashMap();
//Object put(OBject Key,Object value) 将key-value添加(修改)到该集合中
hashMap.put("AA",125);
hashMap.put("BB",122);
hashMap.put("CC",124);
hashMap.put("CC",167);//修改
hashMap.put("DD",124);
System.out.println(hashMap);
System.out.println("=================");
//void putAll(Map m) 将集合map的元素添加到该集合当中
HashMap hashMap1 = new HashMap();
hashMap1.put("lanlan",250);
hashMap1.put("caocao",250);
hashMap.putAll(hashMap1);
System.out.println(hashMap);
System.out.println("=================");
//Object remove(Object key)移除集合中key的元素,返回value 若无,返回null
//boolean remove(Object key,Object value)移除集合中key-value的元素,成功返回true
System.out.println( hashMap.remove("caocao"));
System.out.println(hashMap);
System.out.println("===================");
//clear();清空集合中的元素
hashMap.clear();
System.out.println(hashMap);
}
public static void Test02(){
HashMap hashMap = new HashMap();
hashMap.put("AA",125);
hashMap.put("BB",122);
hashMap.put("CC",124);
hashMap.put("EE",167);
hashMap.put("DD",124);
//Object get(Object key) 获得指定key的value
System.out.println( hashMap.get("EE"));
System.out.println("==================");
//boolean containKey(Object key)
//boolean containValue(Object value) 查询指定的key或value是否在集合当中
System.out.println(hashMap.containsKey("DD"));
System.out.println(hashMap.containsValue(123));
System.out.println("===================");
//int size() 查询集合中key-value的个数
System.out.println(hashMap.size());
System.out.println("====================");
//boolean isEmpty() 查询是否为空
System.out.println(hashMap.isEmpty());
System.out.println("====================");
//boolean equals(Object o) 查询两个集合元素是否相同 ,o是map集合
HashMap hashMap1 = new HashMap();
hashMap1.put("AA",125);
hashMap1.put("BB",122);
hashMap1.put("CC",124);
hashMap1.put("EE",167);
hashMap1.put("DD",124);
System.out.println(hashMap.equals(hashMap1));
}
Map接口的遍历
与其他集合不同,Map接口无法直接使用迭代器。但Map接口提供了entrySet()、 values() 和keySet()方法来将Map接口中的Entry集、value集和key集转化为collection接口或Set接口。在使用迭代器进行遍历。
public static void Test03(){
//Map中无法使用迭代器,
HashMap hashMap = new HashMap();
hashMap.put("AA",125);
hashMap.put("BB",122);
hashMap.put("CC",124);
hashMap.put("EE",167);
hashMap.put("DD",124);
//遍历Map中的key集:keySet()
Set set = hashMap.keySet();
Iterator iterator = set.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历Map中的value集:values()
Collection values = hashMap.values();
Iterator iterator1 = values.iterator();
while(iterator1.hasNext()){
System.out.println(iterator1.next());
}
//遍历所有的Key-value
//方式一:entrySet()
Set set1 = hashMap.entrySet();
Iterator iterator2 = set1.iterator();
while(iterator2.hasNext()){
Object o=iterator2.next();
// System.out.println(iterator2.next());
Map.Entry entry=(Map.Entry) o;
// entry.getKey();
// entry.getValue(); 获得该集合Entry数组的key\value
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
System.out.println("================");
//方式二:
Set set01 = hashMap.keySet();
Iterator iterator03 = set01.iterator();
while(iterator03.hasNext()){
Object o=iterator03.next();
Object o1=hashMap.get(o);
System.out.println(o+"-->"+o1);
}
}
TreeMap
向TreeMap中添加key-value,要求key是同一个类的对象。TreeMap类会对添加的元素按照Key来排序(自然排序或定制排序)。
public static void Test01(){
//自然排序
TreeMap map = new TreeMap();
User u1 = new User("lanlan", 22);
User u2 = new User("caocao", 24);
User u3 = new User("huahua", 25);
map.put(u1,12);
map.put(u2,18);
map.put(u3,121);
Set set01 = map.keySet();
Iterator iterator03 = set01.iterator();
while(iterator03.hasNext()){
Object o=iterator03.next();
Object o1=map.get(o);
System.out.println(o+"-->"+o1);
}
}
public static void Test02(){
//定制排序
TreeMap map = new TreeMap(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof User&& o2 instanceof User){
User u1=(User) o1;
User u2=(User) o2;
return Integer.compare(u1.getSex(),u2.getSex());
}
throw new RuntimeException("输入不匹配");
}
});
User u1 = new User("lanlan", 22);
User u2 = new User("caocao", 24);
User u3 = new User("huahua", 25);
map.put(u1,12);
map.put(u2,18);
map.put(u3,121);
Set set01 = map.keySet();
Iterator iterator03 = set01.iterator();
while(iterator03.hasNext()){
Object o=iterator03.next();
Object o1=map.get(o);
System.out.println(o+"-->"+o1);
}
}
Properties
Properties类作为Hashable子类,主要用来在处理文件。要求key-value都是String类型。
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("jdbc.Properties");
properties.load(fileInputStream);
String name=properties.getProperty("name");
String password=properties.getProperty("password");
System.out.println("name"+name+"password"+password);
}