package com.llb.map;
import java.util.HashMap;
import java.util.Map;
/**
* Map集合常用方法
* 添加 put(key,value)
* 通过key删除 remove(key)
* 查询key是否存在 containsKey(key)
* 查询value是否存在 containsValue(value)
* 集合是否为空 isEmpty()
* 集合大小,长度,元素个数 size()
* 清空集合 clear()
* 获取到所有的 key 存到Set集合中 keySet();
* 通过key获取到value get(key)
*
*/
public class MyMap {
public static void main(String[] args) {
Map<String , String> map = new HashMap<>();
//常用方法
System.out.println("==========常用方法==============");
//添加
map.put("1001","tom");
map.put("1002","jerry");
map.put("1003","spiky");
map.put("1004","hinata");
map.put("1005","张三");
map.put("1006","李四");
System.out.println("被覆盖前的map : "+map);
//如果要添加的key已经存在,会覆盖原来的值,会把原来的值当做返回值进行返回
String naruto = map.put("1004", "naruto");
System.out.println("被覆盖的value : "+naruto);//hinata
System.out.println("被覆盖后的map : "+map);//map : {1004=naruto, 1003=spiky, 1002=jerry, 1001=tom}
//根据key删除对应的value
String remove = map.remove("1001");
System.out.println("被删除的元素 : "+remove);//被删除的元素 : tom
System.out.println("删除元素后的map : "+map);//删除元素后的map : {1004=naruto, 1003=spiky, 1002=jerry}
//判断集合中是否包含指定的key
boolean b = map.containsKey("1004");
boolean b1 = map.containsKey("1010");
System.out.println("是否包含key 1004 : "+b);//true
System.out.println("是否包含key 1010 : "+b1);//false
//判断集合中是否包含指定的value
boolean b2 = map.containsValue("sasuke");//false
boolean b3 = map.containsValue("jerry");//true
System.out.println("是否包含value sasuke : "+b2);
System.out.println("是否包含value jerry : "+b3);
//判断集合是否为空
boolean empty = map.isEmpty();
System.out.println("集合是否为空 : "+empty);//不为空 false
//返回集合的长度
int size = map.size();
System.out.println("集合大小 : "+size);//3
//清空
map.clear();
System.out.println("清空后的集合 : "+map);//{}
//判断集合是否为空
boolean empty1 = map.isEmpty();
System.out.println("集合是否为空 : "+empty1);//为空 true
/**
* 结果
* ==========常用方法==============
* 被覆盖前的map : {1006=李四, 1005=张三, 1004=hinata, 1003=spiky, 1002=jerry, 1001=tom}
* 被覆盖的value : hinata
* 被覆盖后的map : {1006=李四, 1005=张三, 1004=naruto, 1003=spiky, 1002=jerry, 1001=tom}
* 被删除的元素 : tom
* 删除元素后的map : {1006=李四, 1005=张三, 1004=naruto, 1003=spiky, 1002=jerry}
* 是否包含key 1004 : true
* 是否包含key 1010 : false
* 是否包含value sasuke : false
* 是否包含value jerry : true
* 集合是否为空 : false
* 集合大小 : 5
* 清空后的集合 : {}
* 集合是否为空 : true
*/
}
}
//Map集合的遍历
package com.llb.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class MyMap遍历 {
public static void main(String[] args) {
/**
* 创建集合添加元素
* 获取到所有的 key
* 让每一个 key 找到自己对相应的 value
* 打印 key 和 value
*/
//创建集合,添加元素
Map<String, String> map = new HashMap<>();
map.put("001","100");
map.put("002","200");
map.put("003","300");
map.put("004","400");
map.put("005","500");
map.put("006","600");
map.put("007","700");
//获取到所有的key
Set<String> keys = map.keySet();
//遍历set集合得到每一个key
for (String key : keys) {
String value = map.get(key);
System.out.println("key\t"+key+"\tvalue\t"+value);
}
}
}
System.out.println("===============第二种遍历方式=================");
/**
* 第二种遍历方式
* 获取到所有键值对 对象
*
*/
//获取到所有键值对 对象
//Set集合中装的是键值对对象
//Entry里装的是键和值
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key\t"+key+"\tvalue\t"+value);
}
练习
package com.llb.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
public class MyHashMapExe {
public static void main(String[] args) {
HashMap<Student, String> map = new HashMap<>();
Student stu1 = new Student("刘备", 20);
Student stu2 = new Student("关羽", 21);
Student stu3 = new Student("张飞", 22);
map.put(stu1,"江苏");
map.put(stu2,"安徽");
map.put(stu3,"浙江");
Set<Student> students = map.keySet();
for (Student key : students) {
String value = map.get(key);
System.out.println("key\t"+key+"\tvalue\t"+value);
}
System.out.println("========================================");
Set<Map.Entry<Student, String>> entries = map.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println("key\t"+key+"\tvalue\t"+value);
}
System.out.println("=======================================");
map.forEach(
(Student key , String value) -> {
System.out.println("key\t"+key+"\tvalue\t"+value);
}
);
}
}
class Student{
private String name;
private Integer 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 Objects.equals(name, student.name) &&
Objects.equals(age, student.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public Student() {
}
}
/TreeMap
package com.llb.map;
import java.util.Comparator;
import java.util.Objects;
import java.util.TreeMap;
public class MyTreeMap {
public static void main(String[] args) {
TreeMap<Student00,String> tm = new TreeMap<>(new Comparator<Student00>() {
@Override
public int compare(Student00 o1, Student00 o2) {
int res = o2.getAge() - o1.getAge();
res = res == 0 ? o2.getName().compareTo(o1.getName()) : res ;
return res;
}
});
Student00 s1 = new Student00("曹操", 100);
Student00 s2 = new Student00("刘备", 100);
Student00 s3 = new Student00("孙权", 50);
tm.put(s1,"山东");
tm.put(s2,"山西");
tm.put(s3,"河北");
tm.forEach((Student00 key , String value)->{
System.out.println("key\t"+key+" \tvalue\t"+value);
});
}
}
class Student00 /*implements Comparable<Student00>*/{
private String name;
private Integer age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student00 student00 = (Student00) o;
return Objects.equals(name, student00.name) &&
Objects.equals(age, student00.age);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student00(String name, Integer age) {
this.name = name;
this.age = age;
}
public Student00() {
}
/*@Override
public int compareTo(Student00 o) {
int res = this.getAge()-o.getAge();
res = res == 0? this.getName().compareTo(o.getName()) : res ;
return res;
}*/
}
Map HashMap TreeMap
最新推荐文章于 2024-07-24 21:47:59 发布