1.Map集合
地图, 通过一个点可以找到一个具体的位置 映射
map集合也是存数据的
双边队列
Interface Map<K,V>
k: key 键
v: value 值
键值对
意味着咱们map集合中存的数据是键值对像=形式的数据
{0001=家豪, 002=志成, 003=王腾飞}
--|HashMap
--|TreeMap
1.1Ma增:
V put(K key, V vlaue);添加键值对的数据到map集合中
void putAll(Map<? extends K> k, Map<? extends V> v);将一个map集合存放到另外一个map集合中
删:
V remove (K key);通过键删除键值对的数据,返回值是值
改:
V put(K key V value);当键值存在的时候,会将value值覆盖掉的
查:
int size(); 查看集合中元素的个数
boolean isEmpty();判断是否为空,如果不为空返回的是false
boolean containsKey();是否包含这个键
boolean containsValue();是否包含这个值
重要的方法:
V get(K key);通过键获取值
Set<K> keySet();获取map集合中的键,然后存到set集合中
Collection<V> values(); 获取map集合中值,存到了Collection集合中
Set<Map.Entry<K,V>> entrySet();将map集合的键值对,存到了set集合
Map.Entry这个接口的方法
getKey:返回键值对的键
getValue:返回键值对的值p集合中常用的方法
package com.qfedu.a_map;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo1 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("002", "老邢");
map.put("001", "骚磊");
map.put("003", "老万");
//{001=老邢, 002=骚磊, 003=老万}
System.out.println(map);
//键必须是唯一的,因为底层使用的hash算法的操作
//value值是会被替换的,当键一样的时候
map.put("002", "二贝");
System.out.println(map);
Map<String, String> map1 = new HashMap<>();
map1.put("004", "小邢");
map1.put("005", "小骚磊");
map1.put("006", "小老万");
map.putAll(map1);
System.out.println(map);
//{001=骚磊, 002=二贝, 003=老万, 004=小邢, 005=小骚磊, 006=小老万}
System.out.println(map.remove("001"));//骚磊
System.out.println(map);//删除完以后,看看还有咩有这个键值对
map.put("006", "小小万");//修改
System.out.println(map);
//查询
System.out.println(map.size());//5
System.out.println(map.isEmpty());//false
System.out.println(map.containsKey("002"));//true
System.out.println(map.containsValue("大狗蛋"));//false
//获取所有的键 存到set集合中了
Set<String> strings = map.keySet();
System.out.println(strings);
Collection<String> values = map.values();
System.out.println(values);
//entrySet
Set<Map.Entry<String, String>> entries = map.entrySet();
System.out.println(entries);
//将数据取出来
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("--------");
}
}
}
1.2Map集合中的value 存的是对象
package com.qfedu.a_map;
import java.util.*;
class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Demo2 {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 24));
list.add(new Student("狗蛋", 26));
list.add(new Student("财运", 28));
list.add(new Student("彩云", 23));
List<Student> list1 = new ArrayList<>();
list1.add(new Student("二贝", 24));
list1.add(new Student("老邢", 26));
list1.add(new Student("秦始皇", 28));
list1.add(new Student("汉武帝", 23));
Map<String, List<Student>> map1 = new HashMap<>();
map1.put("001", list);
map1.put("002", list1);
//以上在存值
//取值
Collection<List<Student>> values1 = map1.values();
for (List<Student> students : values1) {
for (Student student : students) {
System.out.println(student.name);
}
}
System.out.println("======");
Map<Integer, Student> map = new HashMap<>();
map.put(1, new Student("张三", 24));
map.put(2, new Student("张三", 24));
map.put(3, new Student("狗蛋", 26));
map.put(4, new Student("李四", 21));
System.out.println(map);
//以上存值
//取值
//取键
Set<Integer> key = map.keySet();
System.out.println(key);
Collection<Student> values = map.values();
System.out.println(values);
for (Student value : values) {
System.out.println(value.name);
}
System.out.println("=====");
//使用entrySet
Set<Map.Entry<Integer, Student>> entries = map.entrySet();
for (Map.Entry<Integer, Student> entry : entries) {
System.out.println(entry.getValue().name);
}
}
}