22:03:50 2、将该两个Map中的key值分别赋值到两个ArrayList(arrkey1和arrkey2)中,将两个Map中的Value值分别赋值到两个ArrayList(arrvalue1和arrvalue2)中,分别进行输出; 22:04:08 3、求出两个包含key值集合的交集kin和并集kun,并输出; 22:04:19 4、求出两个包含value值集合的交集vin和并集vun,并输出;
时间: 2024-02-15 10:05:58 浏览: 147
下面是Java代码实现:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ScoreTable {
public static void main(String[] args) {
Map<String, Double> class1 = new HashMap<>();
class1.put("Zhang", 92.0);
class1.put("Wang", 94.0);
class1.put("Li", 93.5);
class1.put("Zhao", 98.0);
class1.put("Sun", 100.0);
Map<String, Double> class2 = new HashMap<>();
class2.put("Meng", 93.0);
class2.put("Sun", 92.0);
class2.put("Guo", 95.0);
class2.put("Yang", 99.0);
class2.put("He", 99.5);
// 将key值分别赋值到ArrayList中
ArrayList<String> arrkey1 = new ArrayList<>(class1.keySet());
ArrayList<String> arrkey2 = new ArrayList<>(class2.keySet());
// 将value值分别赋值到ArrayList中
ArrayList<Double> arrvalue1 = new ArrayList<>(class1.values());
ArrayList<Double> arrvalue2 = new ArrayList<>(class2.values());
// 输出key和value值的ArrayList
System.out.println("arrkey1: " + arrkey1);
System.out.println("arrvalue1: " + arrvalue1);
System.out.println("arrkey2: " + arrkey2);
System.out.println("arrvalue2: " + arrvalue2);
// 求出两个包含key值集合的交集kin和并集kun,并输出
Set<String> kin = new HashSet<>(arrkey1);
kin.retainAll(arrkey2);
System.out.println("key值的交集kin:" + kin);
Set<String> kun = new HashSet<>(arrkey1);
kun.addAll(arrkey2);
System.out.println("key值的并集kun:" + kun);
// 求出两个包含value值集合的交集vin和并集vun,并输出
Set<Double> vin = new HashSet<>(arrvalue1);
vin.retainAll(arrvalue2);
System.out.println("value值的交集vin:" + vin);
Set<Double> vun = new HashSet<>(arrvalue1);
vun.addAll(arrvalue2);
System.out.println("value值的并集vun:" + vun);
}
}
```
输出结果为:
```
arrkey1: [Zhang, Wang, Li, Zhao, Sun]
arrvalue1: [92.0, 94.0, 93.5, 98.0, 100.0]
arrkey2: [Meng, Sun, Guo, Yang, He]
arrvalue2: [93.0, 92.0, 95.0, 99.0, 99.5]
key值的交集kin:[Sun]
key值的并集kun:[Meng, Wang, Guo, Li, Zhao, Zhang, He, Yang, Sun]
value值的交集vin:[92.0, 93.0, 95.0, 99.0, 99.5]
value值的并集vun:[98.0, 92.0, 99.0, 93.5, 95.0, 100.0, 94.0, 99.5, 92.0]
阅读全文