Map集合:键值对对应的双链集合,分为:HashMap集合, Hashtable集合,TreeMap集合。
HashMap集合:采用接口多态的方式来创建对象。
HashMap集合的方法:
添加功能:
hashMap.put();
特点:第一次添加的数据返回的是Null值;而后面添加键相同的数据的时候会发生键相同值覆盖的现象!
HashMap的输入顺序与输入顺序是没有关系的即无序的。
程序举例:
package _11.homework;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo1 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("Lily", 21);
hashMap.put("Lily", 23);
hashMap.put("Sily", 22);
hashMap.put("Sandy", 29);
hashMap.put("Maria", 32);
// Set<String> hashSet=hashMap.keySet();
Set<Entry<String, Integer>> set = hashMap.entrySet();
for (Entry<String, Integer> hashKey : set) {
System.out.println(hashKey);
}
}
程序结果:
//输出结果与输入的顺序无关
Sandy=29
Lily=23//第二次输入相同键,覆盖了21 岁的Lily
Maria=32
Sily=22
删除功能:
方法:
void clear();//移除所有键值对元素
V remove(Object key)://根据键删除相对应的值元素,并把该值返回
String value=hashMap.remove();//根据键名移除相对应键值对并返回
程序举例:
package _11.homework;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo1 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("Lily", 21);
hashMap.put("Lily", 23);
hashMap.put("Sily", 22);
hashMap.put("Sandy", 29);
hashMap.put("Maria", 32);
//hashMap.clear();
// Set<String> hashSet=hashMap.keySet();
Set<Entry<String, Integer>> set = hashMap.entrySet();
for (Entry<String, Integer> hashKey : set) {
System.out.println(hashKey);
}
System.out.println("----------------------------");
System.out.println(hashMap.remove("Sily"));
System.out.println("----------------------------");
Integer value=hashMap.remove("Sandy");
System.out.println(value);
}
}
运行结果:
Sandy=29
Lily=23
Maria=32
Sily=22
----------------------------
22
----------------------------
29
判断功能:
方法:
boolean containsKey(object key)//判断集合是否包含指定的键
boolean containsValue(object value)//判断集合是否包含该值
boolean isEmpty()//判断集合是否为空
程序举例:
package _11.homework;
import java.util.HashMap;
public class HashMapDemo1 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("Lily", 23);
hashMap.put("Sily", 22);
hashMap.put("Sandy", 29);
hashMap.put("Maria", 32);
// hashMap.clear();
System.out.println(hashMap.containsKey("Lily"));
System.out.println("-------");
System.out.println(hashMap.containsValue(28));
System.out.println("-------");
System.out.println(hashMap.isEmpty());
}
}
运行结果:
true
-------
false
-------
false
获取功能:
方法:
Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合
v.get(object key);//根据键获取值
Set<k>keySet();//获取集合中所有键的集合
Collection <v> values();//获取集合中所有值的集合
程序举例:
package _11.homework;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo1 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
hashMap.put("Lily", 23);
hashMap.put("Sily", 22);
hashMap.put("Sandy", 29);
hashMap.put("Maria", 32);
Set<Entry<String,Integer>> set=hashMap.entrySet();
for(Entry<String,Integer> setKey:set){
System.out.println(setKey);
}
System.out.println("---------------------");
Set<String> hashSet=hashMap.keySet();
for(String hashKey:hashSet){
System.out.println(hashKey+"----"+hashMap.get(hashKey));
}
System.out.println("---------------------");
Collection<Integer> value=hashMap.values();
for(Integer keyValue:value){
System.out.println(keyValue);
}
}
}
输出结果:
//Set<Entry<k,v>>.entrySet();//返回一个键值对Set集合
Sandy=29
Lily=23
Maria=32
Sily=22
//v.get(object key);//根据键获取值
//Set<k>keySet();//获取集合中所有键的集合
Sandy----29
Lily----23
Maria----32
Sily----22
//Collection <v> values();//获取集合中所有值的集合
29
23
32
22