目录
一、Map集合概述
1.Map接口的特点
2.方法
二、Map接口的使用
三、Map集合的实现类
1.HashMap
2.Hashtable
3.Properties
4.TreeMap
一、Map集合概述
1.Map接口的特点
(1)用于存储任意键值对
(2)键:无序、无下标、不允许重复(唯一)
(3)值:无序、无下标、允许重复
2.方法
V put(K key,V value); //将对象存入到集合中,关联键值。key重复则覆盖原值。
Object get(Object key)//根据键值获取对应的值。
Set<K> //返回所有的key。
Collection<V> values() //返回包含所有值的Collection集合。
二、Map接口的使用
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<>();
//1.添加元素
map.put("china","中国");
map.put("uk","英国");
map.put("usa","美国");
System.out.println("元素个数:" + map.size());
System.out.println(map.toString());
System.out.println("----------------");
//2.删除
map.remove("usa");
System.out.println("删除后的元素个数:"+map.size());
System.out.println("----------------");
//3.遍历
//(1)使用keySet();
for (String key : map.keySet()){
System.out.println(key+"---"+map.get(key));
}
System.out.println("----------------");
//(2)使用entrySet()方法
for (Map.Entry<String,String> entry : map.entrySet()){
System.out.println(entry.getKey()+"-------"+entry.getValue());
}
System.out.println("----------------");
//4.判断
System.out.println(map.containsKey("cn"));
System.out.println(map.containsValue("中国"));
}
}
三、Map集合的实现类
1.HashMap
线程不安全,运行效率快,允许用null作为key或是value。
测试类:
import java.util.HashMap;
import java.util.Map;
/**
* HashMap集合的使用
* 存储结构:哈希表(数组+链表+红黑树)
* 使用key可hashcode和equals作为重复
*
*/
public class Demo2 {
public static void main(String[] args) {
//创建集合
HashMap<Student,String> students = new HashMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",143546);
Student s2 = new Student("猪八戒",5633421);
Student s3 = new Student("哮天犬",23435);
students.put(s1,"北京");
students.put(s2,"上海");
students.put(s3,"深圳");
students.put(new Student("哮天犬",23435),"南京");
System.out.println("元素个数:"+students.size()); //元素个数:3
System.out.println(students.toString());
System.out.println("--------------------");
//2.删除元素
students.remove(s1);
System.out.println("删除之后的元素个数:"+students.size());
System.out.println("--------------------");
//3.遍历
//(1)使用keySet();
for (Student key : students.keySet()){
System.out.println(key.toString()+"---"+students.get(key));
}
System.out.println("--------------------");
//(2)使用entrySet()方法
for (Map.Entry<Student,String> entry : students.entrySet()){
System.out.println(entry.getKey()+"---"+students.entrySet());
}
System.out.println("--------------------");
//4.判断
System.out.println(students.containsKey(s1));
System.out.println(students.containsValue("北京"));
}
}
学生类:
import java.util.Objects;
public class Student {
private String name;
private int stuNo;
public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return stuNo == student.stuNo && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, stuNo);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stuNo=" + stuNo +
'}';
}
}
2.Hashtable
线程安全,运行效率慢,不允许null作为key或是value。
3.Properties
Hashtable的子类,要求key和value都是String,通常用于配置文件的读取。
4.TreeMap
实现了SortedNap接口(是Map的子接口),可以对key自动排序。
使用:
import java.util.Map;
import java.util.TreeMap;
/**
* TreeMap的使用
* 存储结构:红黑树
*/
public class Demo3 {
public static void main(String[] args) {
//新建集合
TreeMap<Student,String> treeMap = new TreeMap<Student,String>();
//1.添加元素
Student s1 = new Student("孙悟空",143546);
Student s2 = new Student("猪八戒",5633421);
Student s3 = new Student("哮天犬",23435);
treeMap.put(s1,"北京");
treeMap.put(s2,"上海");
treeMap.put(s3,"广东");
System.out.println("元素个数:"+treeMap.size());
System.out.println(treeMap.toString());
System.out.println("-----------------");
//2.删除
treeMap.remove(s1);
System.out.println(treeMap.size());
System.out.println("-----------------");
//3.遍历
//(1)使用keySet
for (Student key : treeMap.keySet()){
System.out.println(key+"--"+treeMap.get(key));
}
System.out.println("-----------------");
for (Map.Entry<Student,String> entry : treeMap.entrySet()){
System.out.println(entry.getKey()+"----"+entry.getValue());
}
System.out.println("-----------------");
//4.判断
System.out.println(treeMap.containsKey(new Student("孙悟空",143546)));
}
}
学生类:
import java.util.Objects;
public class Student implements Comparable<Student>{
private String name;
private int stuNo;
public Student(String name, int stuNo) {
this.name = name;
this.stuNo = stuNo;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
return stuNo == student.stuNo && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, stuNo);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", stuNo=" + stuNo +
'}';
}
@Override
public int compareTo(Student o) {
int n2 = this.stuNo-o.getStuNo(); //只比较学号
return n2;
}
}
定制规则:
重在理解!
感谢ლ(°◕‵ƹ′◕ლ)!!!