Add an element

Report a typo

Read key-value pairs from the standard input and add them to an instance of IdentityHashMap.

The first line is a number of key-value pairs. In each next line, key and value are separated by a whitespace.

Sample Input 1:

3
key value
key1 value
key2 value

Sample Output 1:

key key1 key2
Write a program in Java 17
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int iterations = scanner.nextInt();

Map<String, String> map = ... // inititalize an instance of IdentityHashMap
for (int i = 0; i < iterations; ++i) {
String key = scanner.next();
String value = scanner.next();

... // add key value pair to the "map" object
}

// do not change the code below
System.out.println(String.join(" ", map.keySet().stream().sorted().collect(Collectors.toList())));
}
}
___

Create a free account to access the full topic