Spaces:
Running
Running
HashMap<String, Integer> map = new HashMap<>();
Browse files
Week 3: Objects, files and exceptions/11b. Combine names and grades
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
public static HashMap<String, Integer> combine(ArrayList<String> names, ArrayList<Integer> grades)
|
| 4 |
+
|
| 5 |
+
which takes a list of names and grades as parameters.
|
| 6 |
+
Create and return a new hashmap of names and grades, with names as keys and grades as values.
|
| 7 |
+
The names and grades are listed at the corresponding indexes (i.e., the first name and the first grade belong together).
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
Example execution:
|
| 13 |
+
|
| 14 |
+
public static void main(String[] args){
|
| 15 |
+
ArrayList<String> names = new ArrayList<>();
|
| 16 |
+
ArrayList<Integer> grades = new ArrayList<>();
|
| 17 |
+
|
| 18 |
+
names.add("Axel");
|
| 19 |
+
names.add("Ann");
|
| 20 |
+
names.add("Michael");
|
| 21 |
+
|
| 22 |
+
grades.add(9);
|
| 23 |
+
grades.add(8);
|
| 24 |
+
grades.add(10);
|
| 25 |
+
|
| 26 |
+
HashMap<String, Integer> hm = combine(names, grades);
|
| 27 |
+
System.out.println(hm);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
Program outputs:
|
| 31 |
+
{Axel=9, Michael=10, Ann=8}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
==========================================================
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
import java.util.Random;
|
| 43 |
+
import java.util.ArrayList;
|
| 44 |
+
import java.util.HashMap;
|
| 45 |
+
|
| 46 |
+
public class Test{
|
| 47 |
+
public static void main(String[] args){
|
| 48 |
+
final Random r = new Random();
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
String[] nn = "Jack Joseph Lisa Lena Maya Kim".split(" ");
|
| 52 |
+
|
| 53 |
+
ArrayList<String> nimet = new ArrayList<>();
|
| 54 |
+
ArrayList<Integer> arvosanat = new ArrayList<>();
|
| 55 |
+
|
| 56 |
+
for (String nimi : nn) {
|
| 57 |
+
nimet.add(nimi);
|
| 58 |
+
arvosanat.add(r.nextInt(7) + 4);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
System.out.println("Names: " + nimet);
|
| 62 |
+
System.out.println("Grades: " + arvosanat);
|
| 63 |
+
System.out.println("Combined: ");
|
| 64 |
+
|
| 65 |
+
HashMap<String, Integer> taulu = combine(nimet, arvosanat);
|
| 66 |
+
|
| 67 |
+
for (String nimi : nimet) {
|
| 68 |
+
System.out.println(nimi + ": " + taulu.get(nimi));
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
public static HashMap<String, Integer> combine(ArrayList<String> names, ArrayList<Integer> grades) {
|
| 79 |
+
HashMap<String, Integer> map = new HashMap<>();
|
| 80 |
+
for (int i = 0; i < names.size(); i++) {
|
| 81 |
+
map.put(names.get(i), grades.get(i));
|
| 82 |
+
}
|
| 83 |
+
return map;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
Names: [Jack, Joseph, Lisa, Lena, Maya, Kim]
|
| 98 |
+
Grades: [4, 4, 8, 8, 8, 7]
|
| 99 |
+
Combined:
|
| 100 |
+
Jack: 4
|
| 101 |
+
Joseph: 4
|
| 102 |
+
Lisa: 8
|
| 103 |
+
Lena: 8
|
| 104 |
+
Maya: 8
|
| 105 |
+
Kim: 7
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|