Mega Code Archive

 
Categories / Java Tutorial / Collections
 

How do I Store simple pairs of data for quick lookup

import java.util.HashMap; import java.util.Map; public class MainClass {   public static void main(String args[]) {     String[] names = { "A", "J", "B", "E", "P" };     String[] ids = { "1", "2", "9", "8", "7" };     Map<String, String> IDMap = new HashMap<String, String>();     for (int i = 0; i < names.length; i++)       IDMap.put(ids[i], names[i]);     System.out.println(IDMap.size() + " Students entered: ");     System.out.println(IDMap);   } } 5 Students entered: {7=P, 2=J, 9=B, 8=E, 1=A}