Source of TestHashMapOfStringAndHashSetOfInteger.java


  1: //TestHashMapOfStringAndHashSetOfInteger.java
  2: //Obtained from TestTreeMapOfStringAndTreeSetOfInteger.java
  3: //simply by replacing each instance of TreeMap with HashMap
  4: //and each instance of TreeSet with HashSet.

  6: import java.util.HashMap;
  7: import java.util.HashSet;
  8: import java.util.Scanner;

 10: public class TestHashMapOfStringAndHashSetOfInteger
 11: {
 12:     public static void main(String[] args)
 13:     {
 14:         System.out.println("=====1=========================");
 15:         //Create empty String/HashSet of Integer HashMap, then
 16:         //display content and size.
 17:         HashMap<String, HashSet<Integer>> hashMap;
 18:         hashMap = new HashMap<String, HashSet<Integer>>();
 19:         System.out.println(hashMap);
 20:         System.out.println(hashMap.size());
 21:         pause(1);

 23:         System.out.println("=====2=========================");
 24:         //Create an empty HashSet of Integer and display its content and size.
 25:         HashSet<Integer> hashSetInt;
 26:         hashSetInt = new HashSet<Integer>();
 27:         System.out.println(hashSetInt);
 28:         System.out.println(hashSetInt.size());
 29:         pause(2);

 31:         System.out.println("=====3=========================");
 32:         //Put two integers into the empty HashSet and re-display.
 33:         hashSetInt.add(2);
 34:         hashSetInt.add(5);
 35:         System.out.println(hashSetInt);
 36:         System.out.println(hashSetInt.size());
 37:         pause(3);

 39:         System.out.println("=====4=========================");
 40:         //Put a key/value pair into the HashMap and display.
 41:         hashMap.put("Hello", hashSetInt);
 42:         System.out.println(hashMap);
 43:         System.out.println(hashMap.size());
 44:         System.out.println(hashMap.get("Hello"));
 45:         System.out.println(hashMap.get("Hello").size());
 46:         pause(4);

 48:         System.out.println("=====5=========================");
 49:         //This code segment has no effect because the value set already
 50:         //contains the single value to be added.
 51:         if (hashMap.containsKey("Hello"))
 52:         {
 53:             hashMap.get("Hello").add(5);
 54:         }
 55:         else
 56:         {
 57:             hashSetInt = new HashSet<Integer>();
 58:             hashSetInt.add(5);
 59:             hashMap.put("Hello", hashSetInt);
 60:         }
 61:         System.out.println(hashMap);
 62:         System.out.println(hashMap.size());
 63:         System.out.println(hashMap.get("Hello"));
 64:         System.out.println(hashMap.get("Hello").size());
 65:         pause(5);

 67:         System.out.println("=====6=========================");
 68:         //This code segment does take effect because the value set
 69:         //does not already contain the single value to be added.
 70:         if (hashMap.containsKey("Hello"))
 71:         {
 72:             hashMap.get("Hello").add(12);
 73:         }
 74:         else
 75:         {
 76:             hashSetInt = new HashSet<Integer>();
 77:             hashSetInt.add(12);
 78:             hashMap.put("Hello", hashSetInt);
 79:         }
 80:         System.out.println(hashMap);
 81:         System.out.println(hashMap.size());
 82:         System.out.println(hashMap.get("Hello"));
 83:         System.out.println(hashMap.get("Hello").size());
 84:         pause(6);

 86:         System.out.println("=====7=========================");
 87:         //Add a new key/value pair to the HashMap and re-display.
 88:         if (hashMap.containsKey("Good-bye"))
 89:         {
 90:             hashMap.get("Good-bye").add(20);
 91:         }
 92:         else
 93:         {
 94:             hashSetInt = new HashSet<Integer>();
 95:             hashSetInt.add(20);
 96:             hashMap.put("Good-bye", hashSetInt);
 97:         }
 98:         System.out.println(hashMap);
 99:         System.out.println(hashMap.size());
100:         System.out.println(hashMap.get("Hello"));
101:         System.out.println(hashMap.get("Hello").size());
102:         System.out.println(hashMap.get("Good-bye"));
103:         System.out.println(hashMap.get("Good-bye").size());
104:         pause(7);

106:         System.out.println("=====8=========================");
107:         //Display all keys, all values and all key/value pairs.
108:         //Do it first the pre-Java 8 way ...
109:         for (String s : hashMap.keySet())
110:         {
111:             System.out.print(s + " ");
112:         }
113:         System.out.println();
114:         for (HashSet<Integer> hashSet : hashMap.values())
115:         {
116:             System.out.print(hashSet + " ");
117:         }
118:         System.out.println();
119:         for (String s : hashMap.keySet())
120:         {
121:             System.out.println(s + " " + hashMap.get(s));
122:         }
123:         pause(8);

125:         System.out.println("=====9=========================");
126:         //Display all keys, all values and all key/value pairs.
127:         //Now do it the Java 8 way ...
128:         hashMap.keySet().forEach(s -> System.out.print(s + " "));
129:         System.out.println();
130:         hashMap.values().forEach(i -> System.out.print(i + " "));
131:         System.out.println();
132:         hashMap.keySet().forEach
133:         (
134:             s -> System.out.println(s + " " + hashMap.get(s))
135:         );
136:         pause(9);
137:     }

139:     public static void pause(int pauseNumber)
140:     {
141:         Scanner keyboard = new Scanner(System.in);
142:         System.out.println("Pause " + pauseNumber);
143:         System.out.print("Press Enter to continue ... ");
144:         keyboard.nextLine();
145:     }
146: }
147: /*  Output (without the pauses):
148:     =====1=========================
149:     {}
150:     0
151:     =====2=========================
152:     []
153:     0
154:     =====3=========================
155:     [2, 5]
156:     2
157:     =====4=========================
158:     {Hello=[2, 5]}
159:     1
160:     [2, 5]
161:     2
162:     =====5=========================
163:     {Hello=[2, 5]}
164:     1
165:     [2, 5]
166:     2
167:     =====6=========================
168:     {Hello=[2, 5, 12]}
169:     1
170:     [2, 5, 12]
171:     3
172:     =====7=========================
173:     {Hello=[2, 5, 12], Good-bye=[20]}
174:     2
175:     [2, 5, 12]
176:     3
177:     [20]
178:     1
179:     =====8=========================
180:     Hello Good-bye
181:     [2, 5, 12] [20]
182:     Hello [2, 5, 12]
183:     Good-bye [20]
184:     =====9=========================
185:     Hello Good-bye
186:     [2, 5, 12] [20]
187:     Hello [2, 5, 12]
188:     Good-bye [20]
189: */