Map集合
Map接口特点:
- 用于存储任意键值对(Key-Value);
- 键:无序、无下标、不允许重复(唯一);
- 值:无序、无下标、允许重复。
特点:存储一对数据(Key-Value),无序、无下标、键不可重复、值可以重复。
方法:
V put(K key,V value);//将对象存储到集合中,关联键值。key重复则覆盖原值。
Object get (Object key);//根据键获取对应的值。
keySet <key>;//返回所有key。
Collection<V> values();//返回包含所有值的Collection集合。
Set <Map.Entry<K,V>>; //键值匹配的Set集合。
基本使用:
import java.util.HashMap;
import java.util.Map;
/**
* Map接口的使用
* 特点:(1)存储键值对(2)键不能重复,值可以重复(3)无序
*/
public class Demo01 {
public static void main(String[] args) {
//创建Map集合
Map<String, String> map = new HashMap<>();
//添加元素
map.put("CN","中国");
map.put("UK","英国");
map.put("USA","美国");
// map.put("CN","zhongguo");//值会被替换,键不可以重复
// map.put("China","zhongguo");//值可以重复
System.out.println("元素个数:"+map.size());
System.out.println(map.toString());
//删除
map.remove("USA");
System.out.println("删除之后:"+map.size());
System.out.println(map.toString());
//遍历
//使用keySet();
System.out.println("———————————————keySet()遍历—————————————————");
for (String s : map.keySet()) {
System.out.print(s+"——>"+map.get(s)+"\t");
}
System.out.println("\n———————————————entrySet()遍历(效率更高 )—————————————————");
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.print(entry.getKey()+"-->"+entry.getValue()+"\t");
}
System.out.println();
//判断
System.out.println(map.containsKey("CN"));
System.out.println(map.containsValue("泰国"));
System.out.println(map.isEmpty());
}
}
Map集合实现类
- HashMap【重点】:
- JDK1.2版本,线程不安全,运行效率高 ,允许使用null作为key或者value;
- HashMap();构造一个初始容量为16和默认加载因子为0.75的空HashMap。
- JDK1.2版本,线程不安全,运行效率高 ,允许使用null作为key或者value;
- HashTable:
- JDK1.0版本,线程安全,运行效率慢;不允许null作为key或者value。
- Properties:
- HashTable的子类,要求key和value都是String。通常用于配置文件的读取。
- TreeMap:
- 实现了SortedMap接口(是Map的子接口),可以对key自动排序。
- 实现TreeSet内部调用的就是TreeMap接口。
- TreeSet.add内部调用的就是TreeMap的put方法。
- 实现了SortedMap接口(是Map的子接口),可以对key自动排序。
import java.util.HashMap;
import java.util.Map;
/**
* HashMap集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
*/
public class Demo02 {
public static void main(String[] args) {
//创建对象
HashMap<Student, String> hashMap = new HashMap<>();
Student p1 = new Student("张学友", 20);
Student p2 = new Student("刘德华", 19);
Student p3 = new Student("郭富城", 18);
Student p4 = new Student("黎明", 18);
//添加元素
hashMap.put(p1,"歌神");
hashMap.put(p2,"综合王");
hashMap.put(p3,"舞王");
hashMap.put(p4,"文艺王");
hashMap.put(new Student("张学友",20),"music");
System.out.println("元素个数:"+hashMap.size());
System.out.println(hashMap.toString());
//删除元素
// hashMap.remove(p1);
// hashMap.clear();
// System.out.println("元素个数:"+hashMap.size());
// System.out.println(hashMap.toString());
//遍历
System.out.println("——————————————————keySet方法——————————————————————————");
for (Student student : hashMap.keySet()) {
System.out.print(student.toString()+"-->"+hashMap.get(student)+"\t");
}
System.out.println();
System.out.println("——————————————————entrySet方法——————————————————————————");
for (Map.Entry<Student, String> entry : hashMap.entrySet()) {
System.out.print(entry.getKey()+"-->"+entry.getValue()+"\t");
}
System.out.println();
//判断
System.out.println(hashMap.containsKey(new Student("张学友", 20)));
System.out.println(hashMap.containsKey(p1));
System.out.println(hashMap.containsValue("文艺王"));
}
}
TreeMap:
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
/**
* TreeMap的使用
* 存储结构:红黑树
*/
public class Demo03 {
public static void main(String[] args) {
//创建对象(定制比较)
TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int n1 = o1.getName().compareTo(o2.getName());
int n2 = o1.getStuNo()-o2.getStuNo();
return n1==0?n2:n1;
}
});
Student s1 = new Student("张学友", 20);
Student s2 = new Student("刘德华", 19);
Student s3 = new Student("郭富城", 18);
Student s4 = new Student("黎明", 18);
//新增
treeMap.put(s1,"歌神");
treeMap.put(s2,"综合王");
treeMap.put(s3,"舞王");
treeMap.put(s4,"文艺王");
treeMap.put(new Student("黎明", 18),"永远滴神");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
//删除
// treeMap.remove(s1);
// treeMap.remove(new Student("刘德华", 19));
// treeMap.clear();
// System.out.println("元素个数:"+treeMap.size());
// System.out.println(treeMap.toString());
//遍历
System.out.println("—————————————KeySet———————————————————");
for (Student student : treeMap.keySet()) {
System.out.println(student+"->"+treeMap.get(student));
}
System.out.println("—————————————EntrySet———————————————————");
for (Map.Entry<Student, String> entry : treeMap.entrySet()) {
System.out.println(entry.getKey()+"->"+entry.getValue());
}
//判断
System.out.println(treeMap.containsKey(s1));
System.out.println(treeMap.containsKey(new Student("黎明",18)));
System.out.println(treeMap.containsValue("永远滴神"));
System.out.println(treeMap.isEmpty());
}
}