185k views
3 votes
Package hw1;

import java.util.TreeMap;

public class HW1 {
/**
* Inverts an array of Strings by returning a TreeMap that maps Strings to the smallest index in the array that contains the String.
*
* @param a an array of Strings
* @return a Map that given a String returns the smallest index of the array that contains the String
* (or null if String is not in the array a).
*/
public static TreeMap invert(String[] a) {
//TO DO
return null;
}

/**
* Computes the total number of occurrences of every String in an array.
*
* @param a an array of Strings
* @return a Map that given a String returns the number of occurrences of that String in the array
* (or null if String is not in the array a).
*/
public static TreeMap count(String[] a) {
// TODO
return null;
}
}

User Edward Tan
by
5.6k points

1 Answer

6 votes

Answer:

The Java code is given below with appropriate comments in key areas of the code

Step-by-step explanation:

import java.util.TreeMap;

public class HW1 {

/**

* Inverts an array of Strings by returning a TreeMap that maps Strings to the

* smallest index in the array that contains the String.

*

* @param a an array of Strings

* @return a Map that given a String returns the smallest index of the array

* that contains the String (or null if String is not in the array a).

*/

public static TreeMap<String, Integer> invert(String[] a) {

TreeMap<String, Integer> tmap = new TreeMap<>();

for (int i = 0; i < a.length; i++) {

if (!tmap.containsKey(a[i])) {

tmap.put(a[i], i);

}

}

return tmap;

}

/**

* Computes the total number of occurrences of every String in an array.

*

* @param a an array of Strings

* @return a Map that given a String returns the number of occurrences of that

* String in the array (or null if String is not in the array a).

*/

public static TreeMap<String, Integer> count(String[] a) {

TreeMap<String, Integer> tmap = new TreeMap<>();

for (int i = 0; i < a.length; i++) {

tmap.put(a[i], 1 + tmap.getOrDefault(a[i], 0));

}

return tmap;

}

}

User Taekni
by
5.4k points