Map集合:
里面保留的数据是成对存在的,称之为双列集合,存储的数据,我们叫键值对。之前学的Collection集合叫单列集合。
特点:
- 可以存储两个元素(键值对元素)
- key元素不能重复,value元素可以重复
- 一个key元素只能对应一个value元素
- 存取元素不保证顺序
- 没有索引
java.util.Map(接口类)
HashMap:(哈希表)
LinkedHashMap(链表+哈希表)
TreeMap:底层是红黑树
Map中常用的方法:
V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
把指定的键与指定的值添加到Map集合
V remove(Object key)
Removes the mapping for a key from this map if it is present (optional operation).
把指定的键 所对应的键值对元素在Map集合中删除,返回被删除元素的值
V get(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key.根据指定的键,在Map集合获取对应的值
Set<K> keySet()
Returns a
Set
view of the keys contained in this map.获取Map集合中所有的键,存到Set集合中
Collection<V> values()
获取Map集合中的value元素
boolean containsKey(Object key)
判断改集合是否含有此键
public class test {
public static void main(String[] args) {
//创建Map集合
Map<String,String>map=new HashMap<>();
//往Map添加key value元素
map.put("java","java111");//添加元素
map.put("c++","c++1111");
System.out.println(map);//{c++=c++1111, java=java111}
map.put("java","java222");//修改元素,key值想同,则修改
System.out.println(map);//{c++=c++1111, java=java222}
//删除map集合中的元素,根据key元素
System.out.println(map.remove("java"));//java222
System.out.println(map);//{c++=c++1111}
//获取map集合中的value元素
String rs=map.get("c++");
System.out.println(rs);//c++1111
System.out.println(map.containsKey("c++"));//true
}
}
Map集合遍历:
Map集合不能直接遍历,只能间接遍历
1:键找值
获取Map集合中所有的键,遍历所有的key元素,通过key元素找到对应的value元素
2:键值对
获取集合中所有的Map.Entry(键值对 对象),遍历所又的Map.Entry,通过Entry中的ApI方法取key,value
Set<Map.Entry<K,V>> entrySet()
存储所有键值对对象
public class test2 {
public static void main(String[] args) {
Map<String,String> map=new HashMap<>();
map.put("java","java111");
map.put("c++","c++2222");
map.put("mysql","mysql333");
//遍历通过键找值
//1:增强for循环
Set<String> strings = map.keySet();
for (String string : strings) {
System.out.println("key="+string+" value="+map.get(string));
}
//2:Lambda
map.forEach(new BiConsumer<String, String>() {
@Override
public void accept(String s, String s2) {
System.out.println(map.get(s));
}
});
map.forEach((s,s2)-> System.out.println("key="+s+" value="+map.get(s)));
//遍历通过键值对
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println("key="+entry.getKey()+" value="+entry.getValue());
}
}
}
用HashMap存储自定义类型键
在Map集合中当key存储的是自定义对象时,要保证对象存储数据的唯一性,要重写equals,hashCode方法
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 String toString() {
return "Student{" +
"name='" + name + '\'' +
", 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);
}
//重写hashcode,学生作为键值,名字年龄一样不重复存储,不然地址不同会重复存储
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
public class test3 {
public static void main(String[] args) {
HashMap<Student,String> map=new HashMap<>();
map.put(new Student("aa",19),"tianjin");
map.put(new Student("bb",20),"beijing");
map.put(new Student("cc",17),"hebei");
map.put(new Student("aa",19),"shanghai");//重复,不存储
//遍历:
Set<Student> students = map.keySet();
for (Student student : students) {
System.out.println("key="+student+" value="+map.get(student));
}
/* key=Student{name='cc', age=17} value=hebei
key=Student{name='bb', age=20} value=beijing
key=Student{name='aa', age=19} value=shanghai*/
}
}
LinkedHashMap:
特点:
key元素不重复,无索引,有序(存入的顺序按顺序)。
TreeMap集合:
底层是红黑树,key元素有排序能力,键去重,无索引
练习:通过HashMap集合求字符串字符的存在个数
public class test5 {
public static void main(String[] args) {
HashMap<Character,Integer>map=new HashMap<>();
String target="aababcabcdae";
for(int i=0;i<target.length();i++)
{
char c=target.charAt(i);
if(map.containsKey(c))//存在
{
Integer x=map.get(c);//取出存在次数
x++;//自增
map.put(c,x);
}
else {//不存在,初始次数为一
map.put(c,1);
}
}
Set<Character> characters = map.keySet();
for (Character character : characters) {
System.out.println(character+":"+map.get(character));
}
}
}
集合可以互相嵌套