Write a program that perform hashmap
Program/Input:
Result:
Program/Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.HashMap; import java.util.Iterator; import java.util.Set; public class hashmap { public static void main(String args[]) { String s = "A quick brown fox jumps over the lazy dog."; HashMap<Character, Integer> map = new HashMap<>(); // map[1] = a, 2; // map[2]= b, 1; // map[3] = c, 4; char c = ' '; for (int i = 0; i < s.length(); i++) { c = s.charAt(i); if (map.containsKey(c)) { int cc = map.get(c); map.put(c, ++cc); } else { map.put(c, 1); } } Set ss = map.keySet(); Iterator it = ss.iterator(); while (it.hasNext()) { char ch = (char) it.next(); int r = map.get(ch); System.out.println(ch + " appreas " + r + " time"); // System.out.println(it.next()); } } } |
Result:
A appreas 1 time appreas 8 time a appreas 1 time b appreas 1 time c appreas 1 time d appreas 1 time e appreas 2 time f appreas 1 time g appreas 1 time h appreas 1 time i appreas 1 time j appreas 1 time k appreas 1 time l appreas 1 time m appreas 1 time n appreas 1 time . appreas 1 time o appreas 4 time p appreas 1 time q appreas 1 time r appreas 2 time s appreas 1 time t appreas 1 time u appreas 2 time v appreas 1 time w appreas 1 time x appreas 1 time y appreas 1 time z appreas 1 time
No comments:
Post a Comment