Spaces:
Running
Running
String[] arrKeys = numbers.keySet().toArray(new String[0]); Arrays.sort(arrKeys);
Browse files
Week 3: Objects, files and exceptions/21. Phone book 3: Listing numbers - Extract HASHMAP KEYS, then SORT
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
q21
|
| 2 |
+
Write the method
|
| 3 |
+
|
| 4 |
+
void listBook(HashMap<String,String> numbers
|
| 5 |
+
|
| 6 |
+
which prints the users in the book and their numbers below them on the screen.
|
| 7 |
+
The names should be printed in alphabetical order.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
An example of how to call the method:
|
| 11 |
+
public static void main(String[] args){
|
| 12 |
+
HashMap<String,String> numbers = new HashMap<>();
|
| 13 |
+
numbers.put("Tina", "12345");
|
| 14 |
+
numbers.put("Keith", "54321");
|
| 15 |
+
numbers.put("Jack", "55555");
|
| 16 |
+
|
| 17 |
+
listBook(numbers);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
Program outputs:
|
| 21 |
+
Name: Jack, number: 55555
|
| 22 |
+
Name: Keith, number: 54321
|
| 23 |
+
Name: Tina, number: 12345
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import java.util.Random;
|
| 32 |
+
import java.util.Arrays;
|
| 33 |
+
import java.util.HashMap;
|
| 34 |
+
import java.util.Collections;
|
| 35 |
+
import java.util.ArrayList;
|
| 36 |
+
import java.util.Scanner;
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
public class Test{
|
| 41 |
+
public static void main(String[] args){
|
| 42 |
+
final Random r = new Random();
|
| 43 |
+
|
| 44 |
+
// <hide>
|
| 45 |
+
|
| 46 |
+
HashMap<String, String> numbers = new HashMap<>();
|
| 47 |
+
numbers.put("Pete", "" + r.nextInt(1000000));
|
| 48 |
+
numbers.put("Lena", "" + r.nextInt(1000000));
|
| 49 |
+
numbers.put("Kim", "" + r.nextInt(1000000));
|
| 50 |
+
numbers.put("Laura", "" + r.nextInt(1000000));
|
| 51 |
+
numbers.put("Lisa", "" + r.nextInt(1000000));
|
| 52 |
+
numbers.put("Zorro", "" + r.nextInt(1000000));
|
| 53 |
+
numbers.put("Tarzan", "" + r.nextInt(1000000));
|
| 54 |
+
|
| 55 |
+
listBook(numbers);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
//q21
|
| 61 |
+
public static void listBook(HashMap<String,String> numbers) {
|
| 62 |
+
// https://stackoverflow.com/questions/9047090/how-to-sort-hashmap-keys
|
| 63 |
+
// https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#keySet--
|
| 64 |
+
// sort the HashMap by key
|
| 65 |
+
// String[] arrKeys = numbers.keySet().toArray(); //incorrect
|
| 66 |
+
|
| 67 |
+
// new String[0] is an empty array of type String -> ie we want String[] array, even if empty array
|
| 68 |
+
String[] arrKeys = numbers.keySet().toArray(new String[0]); //correct
|
| 69 |
+
// sort inplace
|
| 70 |
+
Arrays.sort(arrKeys);
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
// correct option 2 - extract keySet as ArrayList, then use Collection to sort
|
| 75 |
+
// Convert keySet to a List and sort it
|
| 76 |
+
// ArrayList<String> arrKeys = new ArrayList<>(numbers.keySet());
|
| 77 |
+
// Collections.sort(arrKeys);
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
// print the sorted HashMap
|
| 83 |
+
for (String key : arrKeys) {
|
| 84 |
+
System.out.println("Name: " + key + ", number: " + numbers.get(key));
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
Name: Kim, number: 701584
|
| 98 |
+
Name: Laura, number: 417806
|
| 99 |
+
Name: Lena, number: 627033
|
| 100 |
+
Name: Lisa, number: 506742
|
| 101 |
+
Name: Pete, number: 292799
|
| 102 |
+
Name: Tarzan, number: 297377
|
| 103 |
+
Name: Zorro, number: 538240
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
|