HashMap के साथ काम करना
कंपनी के employees की डायरेक्टरी रखने के लिए एक HashMap बनाएँ. यानी ऐसी डायरेक्टरी जिसमें Integer ids एक lookup table में String नामों की ओर इशारा करती हों. आप एक नया employee जोड़ेंगे और एक employee को डायरेक्टरी से हटाएँगे.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में डेटा टाइप्स और Exceptions
अभ्यास निर्देश
- एप्लिकेशन में उपयोग के लिए
HashMapइम्पोर्ट करें. - वैरिएबल
directoryकोIntegers औरStrings वालेHashMapके नए instance पर सेट करें. - एक नया employee (नाम
"Marcia"और id31)directoryमैप में जोड़ें. "Davy"को id45के साथdirectoryमैप से हटा दें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
// Import HashMap
import java.____.____;
public class EmployeeDirectory {
public static void main(String[] args) {
// Create a HashMap directory of Integer to String using parameterized constructor
HashMap<_____, ____> directory = new ____<____, ____>();
directory.put(23, "Joye");
// Add employee "Marcia" with id 31 to the directory
directory.____(____, "____");
directory.put(45, "Davy");
System.out.println(directory);
String name = directory.get(31);
System.out.println(name);
// Remove "Davy" from the directory
directory.____(____);
System.out.println(directory);
}
}