Skip to content

Commit 3c708de

Browse files
authored
Map-ds
Java code for map ds
1 parent 19ca972 commit 3c708de

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Examination_map.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
class Examination_map {
5+
6+
public static void main(String [] args) throws Exception {
7+
8+
/* Read input and save as entries in HashMap */
9+
10+
Scanner in = new Scanner(System.in);
11+
System.out.print("Enter no of candidates: ");
12+
int n = in.nextInt(); // Get the number of entries
13+
14+
HashMap<Integer, String> map = new HashMap<>(); // Create the hashMap
15+
16+
System.out.println("\n Input Entries to the List \n");
17+
18+
for (int i = n-1; i >= 0; i -= 1) { // Loop to compare no of candidates and 1, then decreases no of candidates by 1
19+
20+
21+
22+
Scanner logp = new Scanner(System.in); // Get index numbers as variable name'id' to add to hashMap
23+
System.out.println("Index Number: ");
24+
int id = logp.nextInt();
25+
26+
Scanner logn = new Scanner(System.in); // Get names as variable name'name' to add to hashMap
27+
System.out.println("Name: ");
28+
String name = logn.next();
29+
30+
map.put(id, name); // put each name and id to the map (key-id, value-name)
31+
}
32+
33+
System.out.println("\n\tCandidate List\n");
34+
System.out.println("\tIndex No. | Name");
35+
System.out.println("--------------------------------");
36+
37+
38+
for (Map.Entry<Integer, String> entry : map.entrySet()) // Iterating over the hashMap's entry set using for loop
39+
System.out.println("\t"+entry.getKey() + "\t|\t" + entry.getValue()); // print each key-value pairs
40+
41+
System.out.println("\n--------------------------------\n");
42+
43+
System.out.println("\nEnter the index number to get the name");
44+
Scanner nw = new Scanner(System.in); // Get a key element to find the relevent value
45+
Integer d;
46+
// parameter to assign above key element (id2)
47+
48+
49+
/* Read each query and check if its in the HashMap */
50+
while((d = nw.nextInt()) != null) {
51+
52+
if (map.containsKey(d)) { // Check whether the particular key value 's' is being mapped into the hashMap
53+
System.out.println("\n"+d+" Index No 's name: "+(map.get(d))); // Get the relevent value
54+
}
55+
else {
56+
System.out.println("Not found");
57+
}
58+
}
59+
60+
}
61+
}

0 commit comments

Comments
 (0)