HashMap

HashMap class

Read the documentation for Class HashMap<K,V> and answer what methods do the following:

  • adds a key value map in the HashMap
  • gets a value in the map using a key
  • removes the entry for a key
  • returns the number of key-value mappings in the HashMap

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 HashMap

HashMap class – important methods

HashMap<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);

Counting characters

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

Counting characters – Solution 1

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);
            
        }
        
    }

}

Counting characters – Solution 2

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);
            
        }
        
    }

}

Counting characters – Solution 3

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);
            
        }
    }
}

Getting HashMap keys

We can use the method .keySet() to get a set of all keys in a HashMap as an ArrayList

ArrayList<String> myKeys = new ArrayList<String>(myHashMap.keySet());

Then we can sort the ArrayList using Collections

import java.util.Collections; 

We then call the method .sort()

Collections.sort(myKeys);

Modify your counting characters solution to print each character and its count, one per line, in alphabetical order.

Printing sorted key and value – solution

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));
            }
            
        }
    }
}

Counting odds and evens

Write a Java application that given an undetermined number of integers, it creates a HashMap with the count of odds and evens

Counting odds and evens – solution

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);

    }
}