HashMap
Map集合继承体系:
1、Map集合中的元素是一个键值对
2、一个键对应一个值,键不允许重复,键是唯一的
3、值可以发生重复
子类:HashMap<K,V>
成员方法1:
V remove(Object key)
void clear()
boolean containsKey(Object key)
boolean containsValue(Object value)
boolean isEmpty()
int size()
V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
// V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
}
}
这里第一行输出的是null,第三行输出的是小明,,对象的确添加进去了,说明这里返回的是被替代的对象的值
V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
// V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001)); //根据key进行删除
System.out.println(map1);
}
}
若是给了一个不存在的key值,不会报错,会返回null值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
// V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
// V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001)); //根据key进行删除
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
}
}
void clear() 清空
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
// V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
// V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001)); //根据key进行删除
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
System.out.println("=====================");
//void clear()
map1.clear();
System.out.println(map1);
}
}
boolean containsKey(Object key) 是否包含这个Key值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
//V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001));
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
System.out.println("=====================");
//void clear()
// map1.clear();
// System.out.println(map1);
////boolean containsKey(Object key)
System.out.println(map1.containsKey(1003));
}
}
boolean containsValue(Object value) 是否包含这个Value值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
//V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001));
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
System.out.println("=====================");
//void clear()
// map1.clear();
// System.out.println(map1);
System.out.println("=====================");
////boolean containsKey(Object key)
System.out.println(map1.containsKey(1003));
System.out.println("=====================");
//boolean containsValue(Object value)
System.out.println(map1.containsValue("小花"));
}
}
boolean isEmpty() 判断是否是空
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
//V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001));
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
System.out.println("=====================");
//void clear()
// map1.clear();
// System.out.println(map1);
System.out.println("=====================");
////boolean containsKey(Object key)
System.out.println(map1.containsKey(1003));
System.out.println("=====================");
//boolean containsValue(Object value)
System.out.println(map1.containsValue("小花"));
System.out.println("=====================");
//boolean isEmpty()
System.out.println(map1.isEmpty());
}
}
int size() 获取元素个数,键值对的个数
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo1 {
public static void main(String[] args) {
//创建HashMap的对象
//HashMap()
//构造一个空的 HashMap ,默认初始容量(16)和默认负载系数(0.75)
HashMap<Integer, String> map1 = new HashMap<>();
//V put(K key,V value) 向集合中添加一个元素键值对,如果键已经存在集合中,值会进行覆盖处理,返回被覆盖的值
//向map集合中添加一个键值对元素
System.out.println(map1.put(1001,"小明"));
System.out.println(map1);
System.out.println("=====================");
System.out.println(map1.put(1001,"小花"));
System.out.println(map1);
System.out.println("=====================");
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V remove(Object key) 根据键删除整个键值对,因为键是唯一的,返回键对应的值
System.out.println(map1.remove(1001));
System.out.println(map1);
System.out.println(map1.remove(1100));
System.out.println(map1);
System.out.println("=====================");
//void clear()
// map1.clear();
// System.out.println(map1);
System.out.println("=====================");
////boolean containsKey(Object key)
System.out.println(map1.containsKey(1003));
System.out.println("=====================");
//boolean containsValue(Object value)
System.out.println(map1.containsValue("小花"));
System.out.println("=====================");
//boolean isEmpty()
System.out.println(map1.isEmpty());
System.out.println("=====================");
//int size() 获取元素个数,键值对的个数
System.out.println(map1.size());
}
}
成员方法2:
V get(Object key)
Set<K> keySet()
Collection<V> values()
Set<Map.Entry<K,V>> entrySet()
V get(Object key) 根据 键 获取 值
package com.shujia.day14;
import java.util.HashMap;
public class MapDemo2 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V get(Object key) 根据键获取值
System.out.println(map1.get(1010));
}
}
这里因为没有key值为1010的,所以返回的是null值
Set keySet() 获取所有的 键 组成一个Set集合返回
package com.shujia.day14;
import java.util.HashMap;
import java.util.Set;
public class MapDemo2 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V get(Object key) 根据键获取值
System.out.println(map1.get(1010));
System.out.println("=====================");
//Set<K> keySet() 获取所有的键组成一个Set集合返回
Set<Integer> KeySet = map1.keySet();
System.out.println(KeySet);
System.out.println("=====================");
}
}
Collection values()
package com.shujia.day14;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class MapDemo2 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V get(Object key) 根据键获取值
System.out.println(map1.get(1010));
System.out.println("=====================");
//Set<K> keySet() 获取所有的键组成一个Set集合返回
Set<Integer> KeySet = map1.keySet();
System.out.println(KeySet);
System.out.println("=====================");
//Collection<V> values()
Collection<String> values = map1.values();
System.out.println(values);
System.out.println("=====================");
}
}
Collection values() 一次性获取所有的键值对
package com.shujia.day14;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo2 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//V get(Object key) 根据键获取值
System.out.println(map1.get(1010));
System.out.println("=====================");
//Set<K> keySet() 获取所有的键组成一个Set集合返回
Set<Integer> KeySet = map1.keySet();
System.out.println(KeySet);
System.out.println("=====================");
//Collection<V> values()
Collection<String> values = map1.values();
System.out.println(values);
System.out.println("=====================");
//Set<Map.Entry<K,V>> entrySet() 一次性获取所有的键值对
Set<Map.Entry<Integer, String>> entries = map1.entrySet();
System.out.println(entries);
}
}
Map集合遍历的方式:
1、先获取所有的键,遍历键获取对应的值
package com.shujia.day14;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
public class MapDemo3 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//获取所有的键
Set<Integer> keySet = map1.keySet();
for (Integer key:keySet){
//根据键获取对应的值
String value = map1.get(key);
System.out.println(key+"---"+value);
}
}
}
2、直接获取所有的键值对,遍历每一个键值对,就能够得到每一个键和值
package com.shujia.day14;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MapDemo4 {
public static void main(String[] args) {
HashMap<Integer, String> map1 = new HashMap<>();
map1.put(1002,"小红");
map1.put(1003,"小王");
map1.put(1004,"小李");
map1.put(1005,"小白");
System.out.println(map1);
System.out.println("=====================");
//先获取所有的键值对,再进行赋值
Set<Map.Entry<Integer, String>> keyValues = map1.entrySet();
for (Map.Entry<Integer, String> keyValue : keyValues) {
Integer key = keyValue.getKey();
String value = keyValue.getValue();
System.out.println(key+"---"+value);
}
}
}
练习
先创建一个Student1类
package com.shujia.day14;
import java.util.Objects;
public class Student1{
private String name;
private int age;
public Student1() {
}
public Student1(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;
Student1 student1 = (Student1) o;
return age == student1.age && Objects.equals(name, student1.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
// @Override
// public int compareTo(Student1 o) {
// //按照年龄从小到大排序
// int i1 = this.getAge() - o.getAge();
// //隐藏条件:年龄一样的时候,姓名不一定一样
// return (i1 == 0) ? this.getName().compareTo(o.getName()) : i1;
// }
}
package com.shujia.day14;
import java.util.Objects;
public class Student1{
private String name;
private int age;
public Student1() {
}
public Student1(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;
Student1 student1 = (Student1) o;
return age == student1.age && Objects.equals(name, student1.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student1{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
// @Override
// public int compareTo(Student1 o) {
// //按照年龄从小到大排序
// int i1 = this.getAge() - o.getAge();
// //隐藏条件:年龄一样的时候,姓名不一定一样
// return (i1 == 0) ? this.getName().compareTo(o.getName()) : i1;
// }
}
package com.shujia.day14;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
根据昨天HashSet中add方法实际上就是调用今天要讲的HashMap中的put方法,要想保证HashMap中的键唯一
根据分析得出,键的类型要重写hashCode()方法和equals方法
HashMap<Student,String> 当学生的姓名和年龄一样的时候,认为是同一个学生
*/
public class HashMapDemo1 {
public static void main(String[] args) {
//创建集合对象
HashMap<Student1, String> map1 = new HashMap<Student1, String>();
//创建元素对象
Student1 s1 = new Student1("小红", 16);
Student1 s2 = new Student1("小红2", 15);
Student1 s3 = new Student1("小红3", 18);
Student1 s4 = new Student1("小红4", 19);
Student1 s5 = new Student1("小红", 16);
//向集合中添加元素
map1.put(s1, "打篮球");
map1.put(s2, "踢足球");
map1.put(s3, "打排球");
map1.put(s4, "打台球");
map1.put(s5, "打羽毛球");
//遍历集合
Set<Map.Entry<Student1, String>> entries = map1.entrySet();
for (Map.Entry<Student1, String> entry : entries) {
Student1 key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
LinkedHashMap
LinkedHashMap:底层数据结构是哈希表和双链表
取出的顺序与添加的顺序一致
package com.shujia.day14;
import java.util.LinkedHashMap;
/*
LinkedHashMap:底层数据结构是哈希表和双链表
取出的顺序与添加的顺序一致
*/
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
map1.put("1001","shujia1");
map1.put("1002","shujia2");
map1.put("1003","shujia3");
map1.put("1002","shujia4");
map1.put("1007","shujia5");
map1.put("1005","shujia6");
map1.put("1004","shujia7");
System.out.println(map1);
}
}
标签:HashMap,System,put,map1,println,out
From: https://www.cnblogs.com/peculiar/p/18061842