devxlogo

Returning Keywords From a Map

Returning Keywords From a Map

A Hashmap is often used to store Keyword-Value pairs. The keywords are strings, so it is convenient to return a list of the keywords as an array of Strings. Fortunately, there is an easy way to do this! In the following code, the getKeywords() method extracts the keywords as a Set, and then converts them to an array of Strings.

 // The following code will print://  key2//  key1import java.util.HashMap;import java.lang.*; public class KeyVal {  protected HashMap keyValMap;   public KeyVal() {    keyValMap = new HashMap();    keyValMap.put(

devx-admin

Share the Post: