Question Index

Write a program to get the keys as array from the given input by using one of the Map interface methods.

Input (Map) Output (String[])
{A=1, B=2, C=3, D=4, E=5} {A, B, C, D, E}
{Red=-12, Yellow=22, Blue=55, White=33, Green=44} {Red, Yellow, Blue, White, Green}
{Ferrari=1, Mercedes=2, BMW=3, Audi=4, Ford=3} {Ferrari, Mercedes, BMW, Audi, Ford}
null null

import java.util.*;
class GetKeysAsArray
    public static void main(String s[])
        Map<String, Integer> input = new LinkedHashMap<String, Integer>();
        input.put("A", 1);
        input.put("E", 5);
        input.put("B", 2);
        input.put("C", 3);
        input.put("D", 4);
        input.put("A", 6);
        System.out.println("The keys after converted into arry :");
        for (String key : getKeysAsArray(input)) {


    public static String[] getKeysAsArray(Map<String, Integer> input) {




Topic: Java Collection Framework - Collections In Java

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App