Hi Everybody,
I made a small sample about using HashMaps in Java ,and i hope to be useful to everyone what to know how to manipulate with it.
Code :/** * @author : Mohamed Saleh AbdEl-Aziz */ import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class HashListSample { private Map<Integer, Integer> intMap; private Map<String, String> strMap; private Map<Integer, Boolean> boolMap; private Map<Integer, ArrayList> arrListMap; public HashListSample() { intMap = new HashMap<Integer, Integer>(); strMap = new HashMap<String, String>(); boolMap = new HashMap<Integer, Boolean>(); arrListMap = new HashMap<Integer, ArrayList>(); fillBoolMap(); fillIntMap(); fillStrMap(); } public void fillArrayList() { ArrayList<String> objStrArrList = null; ArrayList<Byte> objBytArrList = null; objStrArrList.add("Hello"); objStrArrList.add("World"); objBytArrList.add(Byte.valueOf("12")); objBytArrList.add(Byte.valueOf("78")); arrListMap.put(3333, objStrArrList); arrListMap.put(5555, objBytArrList); } public void fillIntMap() { Integer myIntValues[] = new Integer[]{45, 666, 9778, 14522, 31011, 78}; Integer myIntKeys[] = new Integer[]{1, 2, 33, 4, 5, 66}; for (int x = 0; x < myIntValues.length; x++) { intMap.put(myIntKeys[x], myIntValues[x]); } System.out.println("Integer Map has been filled..."); } public void fillStrMap() { String myStrValues[] = new String[]{"Mohamed", "Saleh", "Java Junior Developer"}; String myStrKeys[] = new String[]{"FN", "MD", "Job"}; for (int x = 0; x < myStrValues.length; x++) { strMap.put(myStrKeys[x], myStrValues[x]); } System.out.println("String Map has been filled..."); } public void fillBoolMap() { Integer myBoolKeys[] = new Integer[]{1, 11, 111, 0}; Boolean myBoolValues[] = new Boolean[]{true, true, true, false}; for (int x = 0; x < myBoolValues.length; x++) { boolMap.put(myBoolKeys[x], myBoolValues[x]); } System.out.println("Boolean Map has been filled..."); } public void doSomeOperations() { //Remove item from Map is depend on the Key value.... System.out.println(); System.out.println("Remove item from Integer Map..."); intMap.remove((Integer) 4); System.out.println(); System.out.println("Remove item from Boolean Map..."); boolMap.remove((Integer) 0); System.out.println(); System.out.println("Remove item from String Map..."); strMap.remove((String) "MN"); } public void clearAllMap() { System.out.println(); System.out.println("Clear all Maps Objects...."); intMap.clear(); boolMap.clear(); strMap.clear(); } public void viewAllInfo() { /**********************************************************************/ System.out.println("Ineger Map size: " + intMap.size()); System.out.println("Boolean Map size: " + boolMap.size()); System.out.println("String Map size: " + strMap.size()); System.out.println(); /**********************************************************************/ //Map DataStructure is getting Data Depend on the key of your Item. System.out.println("View Some of Integer Map Values..."); System.out.println("Item key is {66} in Ineger Map is :" + intMap.get((Integer) 66)); System.out.println("Item key is {4} in Ineger Map is :" + intMap.get((Integer) 4)); System.out.println(); System.out.println("View Some of Boolean Map Values..."); System.out.println("Item key is {1} in Boolean Map is :" + boolMap.get((Integer) 1)); System.out.println("Item key is {0} in Boolean Map is :" + boolMap.get((Integer) 0)); System.out.println(); System.out.println("View Some of String Map Values..."); System.out.println("Item key is {FN} in String Map is :" + strMap.get((String) "FN")); System.out.println("Item key is {Job} in String Map is :" + strMap.get((String) "Job")); } public static void main(String args[]) { HashListSample cls = new HashListSample(); cls.viewAllInfo(); cls.doSomeOperations(); cls.viewAllInfo(); cls.clearAllMap(); cls.viewAllInfo(); } }
