HashMap
classRead the documentation for Class HashMap<K,V>
and answer what methods do the following:
HashMap
class – important methods.put(key, value)
adds a key value map in the HashMap.get(key)
gets a value in the map using a key.remove(key)
removes the entry for a key.size()
returns the number of key-value mappings in the HashMapHashMap
class – important methodsHashMap<String, Integer> soccerPlayers = new HashMap<String, Integer>();
soccerPlayers.put("Pelé", 10)
soccerPlayers.put("Marta", 10)
soccerPlayers.put("Alex Morgan", 13)
soccerPlayers.get("Marta");
soccerPlayers.remove("Pelé");
System.out.println(soccerPlayers.size());
System.out.println(soccerPlayers);
Write a Java application that given a string word
, it creates a HashMap
with single characters as keys, and the count of each character as value
import java.util.HashMap;
public class CountCharacters {
public static void main(String[] args) {
if (args.length > 0) {
String word = args[0];
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
for (int i = 0; i < word.length(); i++) {
Integer previousValue = charCount.get(word.charAt(i));
if (previousValue == null) previousValue = 0;
charCount.put(word.charAt(i), previousValue + 1);
}
System.out.println(charCount);
}
}
}
import java.util.HashMap;
public class CountCharacters {
public static void main(String[] args) {
if (args.length > 0) {
String word = args[0];
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
for (char c : word.toCharArray()) {
Integer previousValue = charCount.get(c);
if (previousValue == null) previousValue = 0;
charCount.put(c, previousValue + 1);
}
System.out.println(charCount);
}
}
}
import java.util.HashMap;
public class CountCharacters {
public static void main(String[] args) {
if (args.length > 0) {
String word = args[0];
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
for (char c : word.toCharArray()) {
charCount.putIfAbsent(c, 0);
charCount.put(c, charCount.get(c) + 1);
}
System.out.println(charCount);
}
}
}
We can use the method .keySet()
to get a set of all keys in a HashMap
as an ArrayList
Then we can sort the ArrayList
using Collections
We then call the method .sort()
Modify your counting characters solution to print each character and its count, one per line, in alphabetical order.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class CountCharacters {
public static void main(String[] args) {
if (args.length > 0) {
String word = args[0];
HashMap<Character, Integer> charCount = new HashMap<Character, Integer>();
for (char c : word.toCharArray()) {
charCount.putIfAbsent(c, 0);
charCount.put(c, charCount.get(c) + 1);
}
ArrayList<Character> allChars = new ArrayList<Character>(charCount.keySet());
Collections.sort(allChars);
for (Character c : allChars) {
System.out.println(c + " - " + charCount.get(c));
}
}
}
}
Write a Java application that given an undetermined number of integers, it creates a HashMap
with the count of odds and evens
import java.util.HashMap;
public class CountOddsEvens {
public static void main(String[] args) {
HashMap<String, Integer> oddEvenCount = new HashMap<String, Integer>();
oddEvenCount.put("odds", 0);
oddEvenCount.put("evens", 0);
for (String arg : args) {
int n = Integer.valueOf(arg);
if (n % 2 == 0) oddEvenCount.put("evens", oddEvenCount.get("evens") + 1);
else oddEvenCount.put("odds", oddEvenCount.get("odds") + 1);
}
System.out.println(oddEvenCount);
}
}