一:Map集合概述和特点
Map接口概述:将键映射到值的对象一个映射不能包含重复的键每个键最多只能映射到一个值。
Map接口和Collection接口的不同:
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效
Map集合的功能概述:
添加功能:
V put(K key,V value):添加元素:如果键是第一次存储,就直接存储元素,返回null如果键不是第一次存在,
就用值把以前的值替换掉,返回以前的值
删除功能:
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
判断功能:
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
获取功能:
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
长度功能:
int size():返回集合中的键值对的对数.
Map集合的两种遍历方式:
1 :KeySet() 键找值:把集合中所有的键放入一个单列的Set集合中,然后遍历这个Set集合中的键,
在根据每个键找 其所对应的值。
2 :Entry()方法: 把Map集合中所有的键值对当做键值对对象存入到一个单列结合,在遍历该单列集合中
的所有键值对对象,有该对象调用getKey()和getValue()方法来获取多对应的键和值。
两种遍历方式的举例:
package org.westos.Demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo{
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<>();
Student s1 = new Student("孙悟空", 20);
Student s2 = new Student("猪八戒", 22);
Student s3 = new Student("沙僧", 23);
Student s4 = new Student("唐僧", 24);
Student s5 = new Student("如来", 30);
hm.put(s1,"0x001");
hm.put(s2,"0x002");
hm.put(s3,"0x003");
hm.put(s4,"0x004");
hm.put(s5,"0x005");
//第一种遍历方式:值查找
Set<Student> students = hm.keySet();
for(Student student:students){
String s = hm.get(student);
System.out.println(student.getName()+"---"+student.getAge()+"---"+s);
}
System.out.println("-------------------------");
//第二种遍历方式:entrySet()
Set<Map.Entry<Student, String>> entries = hm.entrySet();
for(Map.Entry<Student, String> entry:entries){
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName()+"----"+key.getAge()+"---"+value);
}
}
}
package org.westos.Demo;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
二:HashMap
基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了非同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序, 特别是它不保证该顺序恒久不变,Map集合所有的数据结构,只跟键有关
注:如果键是自定义类的对象,要实现唯一性必须重写equals()方法和HashCode()方法;
package org.westos.demo3;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapDemo3 {
public static void main(String[] args) {
//存键是String 值是Student
HashMap<String, Student> hm = new HashMap<>();
hm.put("s001",new Student("张曼玉",30));
hm.put("s002",new Student("王祖贤",35));
hm.put("s003",new Student("朱茵",38));
hm.put("s004", new Student("朱茵", 38));
//遍历
Set<String> strings = hm.keySet();
for(String key:strings){
Student student = hm.get(key);
System.out.println(key+"==="+student.getName()+"==="+student.getAge());
}
System.out.println("----------------");
//方式2 来遍历
Set<Map.Entry<String, Student>> entries = hm.entrySet();
for(Map.Entry<String, Student> entry:entries){
String key = entry.getKey();
Student value = entry.getValue();
System.out.println(key+"==="+value.getName()+"=="+value.getAge());
}
}
}
package org.westos.demo3;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
三:HashLinkMap
LinkedHashMap的概述: Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序
LinkedHashMap的特点: 底层的数据结构是链表和哈希表 元素有序 并且唯一元素的有序性由链表数据结构保证
唯一性由 哈希表数据结构保证,Map集合的数据结构只和键有关
package org.westos.demo5;
import java.util.LinkedHashMap;
import java.util.Set;
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<Integer, String> hashMap = new LinkedHashMap<>();
hashMap.put(1,"张三");
hashMap.put(2, "李四");
hashMap.put(3, "王五");
hashMap.put(4, "赵六");
hashMap.put(8, "印度阿三");
Set<Integer> integers = hashMap.keySet();
for(Integer key:integers){
System.out.println(key+"=="+hashMap.get(key));
}
}
}
四:TreeMap
TreeMap的概述: 键的数据结构是红黑树,可保证键的排序和唯一性 排序分为自然排序和比较器排序
线程是不安全的效率比较高
TreeMap排序的两种方法
自然排序:
Comparable<T>:如果键值是自定义类的对象,则该类一定得实现Comparable<T>接口并且重写其中的compareTo()方法
public int compareTo(T t) : 比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数。
比较器排序:
Comparator<T>:调用有参构造传入一个比较器,采用匿名内部类的方法并且重写其中的compare()方法
public int compare(T t1, T t2): 比较用来排序的两个参数。根据第一个参数小于、等于或大于第二个参数分别返回负整数、零或正整数。
两种排序的举例:
package org.westos.Demo;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
//3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历
public class ZuoYe3 {
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int r = o1.getAge() - o2.getAge();
int r1 = r == 0 ? o1.getName().compareTo(o2.getName()) : r;
return r1;
}
});
Student s1 = new Student("喜洋洋", 10);
Student s2 = new Student("美羊羊", 20);
Student s3 = new Student("沸羊羊", 19);
Student s4 = new Student("懒洋洋", 29);
Student s5 = new Student("小灰灰", 13);
tm.put(s1, "0x001");
tm.put(s2, "0x002");
tm.put(s3, "0x003");
tm.put(s4, "0x004");
tm.put(s5, "0x005");
//第一种遍历方式 Keyset()键找值
Set<Student> students = tm.keySet();
for (Student student : students) {
String s = tm.get(student);
System.out.println(student.getName() + "---" + student.getAge() + "---" + s);
}
System.out.println("--------------------------------------------");
//第二中遍历方式 entryset()
Set<Map.Entry<Student, String>> entries = tm.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName() + "---" + key.getAge() + "---" + value);
}
}
}
package org.westos.Demo;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
集合嵌套集合的举例:
ackage org.westos.Demo;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
// 基础班
// 张三 20
// 李四 22
// 就业班
// 王五 21
// 赵六 23
// HashMap嵌套HashMap
// HashMap嵌套ArrayList
public class ZuoYe4 {
public static void main(String[] args) {
HashMap<String, Integer> hm = new HashMap<>();
hm.put("张三", 20);
hm.put("李四", 22);
HashMap<String, Integer> hm1 = new HashMap<>();
hm1.put("王五", 21);
hm1.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> HM = new HashMap<>();
HM.put("基础班", hm);
HM.put("就业办", hm1);
//第一种遍历方式
Set<String> strings = HM.keySet();
for (String string : strings) {
System.out.println(string);
HashMap<String, Integer> stringIntegerHashMap = HM.get(string);
Set<String> strings1 = stringIntegerHashMap.keySet();
for (String st : strings1) {
Integer integer = stringIntegerHashMap.get(st);
System.out.println("\t" + st + "\t\t" + integer);
}
}
System.out.println("--------------------------------------------");
//第二种遍历方式
Set<Map.Entry<String, HashMap<String, Integer>>> entries = HM.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
System.out.println(key);
HashMap<String, Integer> value = entry.getValue();
Set<Map.Entry<String, Integer>> entries1 = value.entrySet();
for (Map.Entry<String, Integer> entry1 : entries1) {
String key1 = entry1.getKey();
Integer value1 = entry1.getValue();
System.out.println("\t" + key1 + "\t\t" + value1);
}
}
}