Map集合应用举例
1.遍历
创建学生类
public class Student {
private String name;
private int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
}
键找值遍历
键值对对象找键和值遍历
HashMap<Student, String> hashMap = new HashMap<>();
hashMap.put(new Student("小明",10), "123");
hashMap.put(new Student("小红",9), "213");
hashMap.put(new Student("小强",12), "321");
hashMap.put(new Student("大韩",11), "444");
//键找值遍历
Set<Student> set = hashMap.keySet();
for (Student student : set) {
String s = hashMap.get(student);
System.out.println(student.getName()+"--"+student.getAge()+"--"+s);
}
//键值对对象找键和值
Set<Map.Entry<Student, String>> entries = hashMap.entrySet();//键值对对象
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();//键值对对象找键
String value = entry.getValue();//键值对对象找值
System.out.println(key.getName()+"--"+key.getAge()+"--"+value);
}
2.TreeMap集合排序
创建学生类并重写compareTo方法
public class Student implements Comparable<Student>{
private String name;
private int 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
@Override
public int compareTo(Student o) {
int num = this.age - o.getAge();
int num1 = num == 0? this.name.compareTo(o.name):num;
int num2 = num1 == 0? this.name.length()- o.name.length():num1;
return num2;
}
}
(1)自然排序
public static void main(String[] args) {
TreeMap<Student, String> treeMap = new TreeMap<>();
treeMap.put(new Student("小高11",10), "123");
treeMap.put(new Student("小高",10), "123");
treeMap.put(new Student("小加",15), "123");
treeMap.put(new Student("小红",9), "123");
treeMap.put(new Student("小梁",12), "123");
Set<Student> keys = treeMap.keySet();
for (Student key : keys) {
String s = treeMap.get(key);
System.out.println(key.getName()+"--"+key.getAge()+"--"+s);
}
}
(2)比较器排序,重写comparator方法
TreeMap<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num = o1.getAge() - o2.getAge();
int num1 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
int num2 = num1 == 0 ? o1.getName().length() - o2.getName().length() : num1;
return num2;
}
});
treeMap.put(new Student("小明",10), "123");
treeMap.put(new Student("小红",9), "213");
treeMap.put(new Student("小强",12), "321");
treeMap.put(new Student("大韩",11), "444");
Set<Map.Entry<Student, String>> entries = treeMap.entrySet();
for (Map.Entry<Student, String> entry : entries) {
Student key = entry.getKey();
String value = entry.getValue();
System.out.println(key.getName()+"--"+key.getAge()+"--"+value);
}
3.集合嵌套
打印出如下内容
就业班
王五--21
赵六--23
基础班
李四--22
张三--20
先创建学生类
(1)HashMap嵌套HashMap
public static void main(String[] args) {
HashMap<String, Integer> student1 = new HashMap<>();
student1.put("张三", 20);
student1.put("李四", 22);
HashMap<String, Integer> student2 = new HashMap<>();
student2.put("王五", 21);
student2.put("赵六", 23);
HashMap<String, HashMap<String, Integer>> room = new HashMap<>();
room.put("基础班", student1);
room.put("就业班", student2);
Set<Map.Entry<String, HashMap<String, Integer>>> entries = room.entrySet();
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
System.out.println(entry.getKey());
Set<Map.Entry<String, Integer>> entries1 = entry.getValue().entrySet();
for (Map.Entry<String, Integer> entry1 : entries1) {
String key = entry1.getKey();
Integer value = entry1.getValue();
System.out.println("\t"+key+"--"+value);
}
}
(2)HashMap嵌套ArrayList
ArrayList<Student> list = new ArrayList<>();
list.add(new Student("张三", 20));
list.add(new Student("李四", 22));
ArrayList<Student> list1 = new ArrayList<>();
list1.add(new Student("王五",21));
list1.add(new Student("赵六",23));
HashMap<String, ArrayList<Student>> map = new HashMap<>();
map.put("基础班", list);
map.put("就业班", list1);
Set<Map.Entry<String, ArrayList<Student>>> entries = map.entrySet();
for (Map.Entry<String, ArrayList<Student>> entry : entries) {
System.out.println(entry.getKey());
for (int i = 0; i < entry.getValue().size(); i++) {
Student student = entry.getValue().get(i);
System.out.println("\t"+student.getName()+"--"+student.getAge());
}
}