Write the output produced when the following methods is pass

Write the output produced when the following methods is passed each of the following maps:

public static void mystery(Map<String, String> m) {

Set<String> s = new TreeSet(String);

for (String key : m.keySet()){

if (!m.get(key).equals(key)) {

s.add(m.get(key));

} else {

s.remove(m.get(key));

}

}

System.out.println(s);

}

a. {sheep=wool, house=brick, cast=plaster, wool=wool}

b. {ball=blue, winkie=yellow, corn=yellow, grass=green, emerald=green}

c. {pumpkin=peach, corn =apple, apple=apple, pie=fruit, peach=peach}

d. {lab=ipl, lion=cat, corgi=dog, cat=cat, emu=animal, nyan=cat}

Solution

Answers:

a. {sheep=wool, house=brick, cast=plaster, wool=wool} will print [brick, plaster]

b. {ball=blue, winkie=yellow, corn=yellow, grass=green, emerald=green} will print [blue, green, yellow]

c. {pumpkin=peach, corn =apple, apple=apple, pie=fruit, peach=peach} will print [apple, fruit]

d. {lab=ipl, lion=cat, corgi=dog, cat=cat, emu=animal, nyan=cat} will print [animal, cat, dog, ipl]

Write the output produced when the following methods is passed each of the following maps: public static void mystery(Map<String, String> m) { Set<Stri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site