A Map <String,Integer> can be thought of as a function which maps Strings to Integers.
For example, if we run WordSort on rrr we get:
ape : 3 cat : 1 dog : 1 paper : 2 rabbit : 3 schlongle : 1
This function can be inverted to a function which maps Integers to Sets of Strings as follows:
1 : [cat, schlongle, dog] 2 : [paper] 3 : [rabbit, ape]
Write a method whose heading is:
public static TreeMap <Integer,HashSet <String>> invert (TreeMap<String,Integer> t)which produces a TreeMap which inverts the TreeMap in the way described. week5/WordSort1.java
TreeMap <String,Character> map = new TreeMap <String,Character>( );Watch video about easy assignment for week 5
public class pair <I,S> {
I i;
S s;
public pair (I i1, S s1)
{
i=i1; s=s1;
}
public String toString()
{
return "("+i+","+s+")";
}
}
Complete the following generic class:
public class mapToSetPair <A,B> {
public HashSet <pair<A,B>> make (TreeMap <A,HashSet <B>> t)
{
}
}
Which from a function from A to Set(B) produces a HashSet of pairs
in A X B.
e.g. applied to rrr gives the set of pairs:
(a,apple) (r,rock) (c,cat) (r,rabbit) (a,ape) (s,schlongle) (d,dog) (p,paper)
Adapt WordSort4.java to use it. Solution
s.danicic@gold.ac.uk