import java.util.HashMap;
import java.util.Map;

/**
 * Trivial example of using a map.
 * Maps the argument to the length of the argument
 * 
 * @author CSCI209
 *
 */
public class MapExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	    // State the key's type and the value's type
		Map<String, Integer> argToLength = new HashMap<>();

		for (String arg : args) {
			// if there are duplicate arguments, this will write over that entry in the map.
			argToLength.put(arg, arg.length());
		}
		
		// display the results
		System.out.println("Arguments' Lengths: ");
		for( String arg : argToLength.keySet() ) {
			System.out.println(arg + " --> " + argToLength.get(arg));
		}
		
		// ------- Another Example --------
		// count the arguments
		Map<String, Integer> argToCount = new HashMap<>();
		
		System.out.println("\n");

		for (String arg : args) {
			// if there are duplicate arguments, this will write over that entry in the map.
			if( argToCount.containsKey(arg) ) {
			    int oldCount = argToCount.get(arg);
			    // showing using put and getting the old value back
			    int oldValue = argToCount.put(arg, oldCount + 1);
			    System.out.println("The old value for " + arg + " was " + oldValue);
			} else {
			    argToCount.put(arg, 1);   
			}
		}
		
		// display the results
		System.out.println("\nArguments' Number of Occurrences: ");
		for( String arg : argToLength.keySet() ) {
			System.out.println(arg + " --> " + argToCount.get(arg));
		}

	}

}
