Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

solve it with JAVA: Write the output that is printed when the given method below is passed each of the following maps and lists as

solve it with JAVA:

Write the output that is printed when the given method below is passed each of the following maps and lists as its parameters. Recall that maps print in a {key1=value1, key2=value2, ..., keyN=valueN} format.

Though hash maps usually have unpredictable ordering, for this problem, you should assume that when looping over the map or printing a map, it visits the keys in the order that they were added to the map or the order they are declared below. If a map calls put() on a key that already exists, it retains its current position in the ordering.

public static void collectionMystery5(ArrayList list1, ArrayList list2) {
 HashMap result = new HashMap();
 
 for (int i = 0; i < list1.size(); i++) {
 String s1 = list1.get(i);
 String s2 = list2.get(i);
 
 if (!result.containsKey(s1)) {
 result.put(s1, s2);
 } else if (!result.containsKey(s2)) {
 result.put(s2, s1);
 } else {
 result.put(s1 + s2, s1);
 }
 }
 System.out.println(result);
}

list1 = ["cat", "cat", "long", "long", "longcat"]

list2 = ["mew", "purr", "cat", "cat", "purr" ]

list1 = ["a", "b", "a", "ab", "ab", "y", "abb"]

list2 = ["b", "c", "b", "b", "c", "abb", "y" ]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions