实现对HashMap中的value进行排序
package com.wz.work;
import java.util.*;
public class test01 {
/**
* 需求:实现对HashMap中的value进行排序
* @param args
*/
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("张三",18);
map.put("李四",20);
map.put("王五",26);
map.put("赵六",21);
map.put("吴七",19);
map.put("王八",22);
//获取映射对象集合
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
//将Set集合中的所有元素添加到list
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<>(entrySet);
//利用ArrayList的方法进行排序
list.sort(new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return Integer.compare(o1.getValue(),o2.getValue());
}
});
//遍历list
for (Map.Entry<String,Integer> entry:list
) {
System.out.println(entry);
}
}
}
1. Map
1. LinkedHashMap
clss LinkedHashMap extends HashMap
特点:有序+key去重
2. Hashtable
特点:无序+key去重+线程安全(方法上加锁)
3. ConcurrentHashMap
特点:无序+key去重+线程安全(局部加锁+CAS,效率更高)
4. 区别
特点的区别:
类型 | 特点的区别 |
HashMap | 无序+Key去重 |
LinkedHashMap | 有序+key去重 |
Hashtable | 无序+key去重+线程安全(方法上加锁) |
ConcurrentHashMap | 无序+key去重+线程安全(局部加锁+CAS,效率更高) |
存储空键、空值的区别:
类型 | 存储空键、空值的区别 |
HashMap | ok |
LinkedHashMap | ok |
Hashtable | no |
ConcurrentHashMap | no |
5. TreeMap
针对key值进行自然排序
package com.wz.treemap_class;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class test02 {
public static void main(String[] args) {
TreeMap<String, Integer> map = new TreeMap<>();
map.put("c",10);
map.put("a",20);
map.put("b",30);
map.put("d",40);
map.put("f",50);
map.put("e",60);
map.put("e",70);
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry:entrySet
) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
需求:创建TreeMap对象,key存入学生对象,value存入学生爱好。
a:使用内置比较器,按照学生年龄排序
package com.wz.treemap_class;
public class Student implements Comparable<Student>{
private String name;
private char sex;
private int age;
private String classId;
private String id;
public Student() {
}
public Student(String classId, String id) {
this.classId = classId;
this.id = id;
}
public Student(String name, int age, char sex, String classId, String id) {
this.name = name;
this.sex = sex;
this.age = age;
this.classId = classId;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getClassId() {
return classId;
}
public void setClassId(String classId) {
this.classId = classId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj instanceof Student){
Student stu = (Student) obj;
if(classId.equals(stu.classId) && id.equals(stu.id)){
return true;
}
}
return false;
}
@Override
public String toString() {
return name + "\t" + sex + "\t" + age + "\t" + classId + "\t" + id;
}
//排序规则:按照年龄排序
@Override
public int compareTo(Student o) {
//TreeSet场景中:
//this表示要添加的对象
//o表示在TreeSet中已经存储的对象
return this.age - o.age;
}
}
package com.wz.treemap_class;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class test03 {
public static void main(String[] args) {
TreeMap<Student, String> map = new TreeMap<>();
map.put(new Student("张三",18,'男',"2023001","001"),"打游戏");
map.put(new Student("李四",19,'女',"2023002","002"),"听音乐");
map.put(new Student("王五",17,'男',"2023003","003"),"打篮球");
map.put(new Student("赵六",20,'女',"2023004","004"),"踢足球");
map.put(new Student("吴七",19,'男',"2023005","005"),"打乒乓球");
map.put(new Student("王八",22,'女',"2023006","006"),"打王者荣耀");
Set<Map.Entry<Student, String>> entrySet = map.entrySet();
for (Map.Entry<Student, String> entry:entrySet
) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}
b:使用外置比较器,按照学生姓名的长度排序,长度一致按照年龄排序
package com.wz.treemap_class;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class test03 {
public static void main(String[] args) {
TreeMap<Student, String> map = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if(o1.equals(o2)){
return 0;
}
int nameLen1 = o1.getName().length();
int nameLen2 = o2.getName().length();
if(nameLen1 != nameLen2){
return Integer.compare(nameLen1, nameLen2);
}
int age1 = o1.getAge();
int age2 = o2.getAge();
if(age1 != age2){
return Integer.compare(age1,age2);
}
return 1;
}
});
map.put(new Student("张三",18,'男',"2023001","001"),"打游戏");
map.put(new Student("李四",19,'女',"2023001","002"),"听音乐");
map.put(new Student("王五",17,'男',"2023001","003"),"打篮球");
map.put(new Student("赵六",20,'女',"2023001","004"),"踢足球");
map.put(new Student("吴七",19,'男',"2023001","005"),"打乒乓球");
map.put(new Student("王八",22,'女',"2023002","001"),"打王者荣耀");
map.put(new Student("AAA",25,'男',"2023002","002"),"打篮球");
map.put(new Student("BBB",16,'女',"2023002","003"),"踢足球");
map.put(new Student("CCC",22,'男',"2023002","004"),"打乒乓球");
map.put(new Student("DDDD",19,'女',"2023002","005"),"打王者荣耀");
Set<Map.Entry<Student, String>> entrySet = map.entrySet();
for (Map.Entry<Student, String> entry:entrySet
) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"---"+value);
}
}
}