双列结合

HashMap

image-20231121143051936

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Day2.class1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class demol1 {
public static void main(String[] args) {
HashMap<student,String> HM=new HashMap<>();
student s1=new student("zhangsang",12);
student s2=new student("Lisi",23);
student s3=new student("wangwu",22);
HM.put(s1,"福建");
HM.put(s2,"北京");
HM.put(s3,"上海");

//对集合进行遍历的三种方式
//通过主键查找
Set<student> students = HM.keySet();
for (student student : students) {
//System.out.println(student);
System.out.println(student+HM.get(student));
}
System.out.println("--------------------------------------");
Set<Map.Entry<student, String>> entries = HM.entrySet();
for (Map.Entry<student, String> entry : entries) {
System.out.println(entry.getKey()+entry.getValue());
}
System.out.println("--------------------------------------");
HM.forEach(( key, value)-> System.out.println(key+value));

}
}

Map

image-20231121143223825

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Day2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.BiConsumer;

public class demol12 {
public static void main(String[] args) {
String []arr={"A","B","C","D"};
//定义一个集合接受投票记录
ArrayList<String> AL=new ArrayList<>();
Random rd=new Random();
for (int i = 0; i < 80; i++) {
int a= rd.nextInt(4);
AL.add(arr[a]);
}
System.out.println(AL);
HashMap<String,Integer> hm=new HashMap<>();
for (String s : AL) {
if(hm.containsKey(s)){
Integer num = hm.get(s);
num++;
hm.put(s,num);
}else {
hm.put(s,1);
}
}
//遍历集合
hm.forEach(( k, v)-> System.out.println(k+v));
//找最大值
Integer max=0;
for (Map.Entry<String, Integer> ah : hm.entrySet()) {
if(max< ah.getValue())max=ah.getValue();
}
System.out.println("最高票数为:"+max);
for (Map.Entry<String, Integer> e : hm.entrySet()) {
if(e.getValue()==max) System.out.println(e.getKey()+"景点想去的人最多");
}
}
}

TreeMap

image-20231122153032832

需求一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package Day2;

import java.util.Comparator;
import java.util.TreeMap;

public class demol13 {

public static void main(String[] args) {
TreeMap<Integer,String> Thing=new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
Thing.put(1,"饼干");
Thing.put(2,"面包");
Thing.put(3,"小卡车");
Thing.put(5,"玩偶");
Thing.put(4,"辣条");
System.out.println(Thing);


}
}

需求二

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package Day2.class2;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class text {
public static void main(String[] args) {
TreeMap<student,String> p=new TreeMap<>(( o1, o2)-> {
int i=o1.getAge()-o2.getAge();
i=i==0?o1.getName().compareTo(o2.getName()):i;
return i;
});
student s1=new student("zhan",17);
student s2=new student("Li",13);
student s3=new student("wanwu",13);
student s4=new student("qiao",13);
p.put(s1,"深圳");
p.put(s2,"北京");
p.put(s3,"上海");
p.put(s4,"广州");
for (Map.Entry<student, String> a : p.entrySet()) {
System.out.println(a.getKey());
System.out.println(a.getValue());
}
}

}

统计个数

image-20231122161000822

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package Day2;

import java.util.Map;
import java.util.TreeMap;

public class demol15 {
public static void main(String[] args) {
String s="aababcabcdabcde";
//定义集合存储字母
TreeMap<Character,Integer> tm=new TreeMap<>();
//遍历得到字符串的每一个值
for (int i = 0; i < s.length(); i++) {
char c=s.charAt(i);
if(tm.containsKey(c)){
Integer e = tm.get(c);
e++;
tm.put(c,e);
}else{
tm.put(c,1);
}
}
//输出
System.out.println(tm);

}
}

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝