In: Computer Science
import java.util.HashMap;
import java.util.Map;
public class ExampleOne {
public static void main(String[] args) {
String str[]= {"banana", "apples", "blueberry", "orange"};
System.out.println(firstOne(str));
}
public static Map<String, Integer> firstOne(String[] str) {
Map<String, Integer> map = new HashMap<String, Integer>();
//Iterating thorugh the given array of strings
for (String s : str) {
//checking if string is empty than not processing
if (s.trim().length() == 0)
continue;
//extracting the first char
String firstChar = s.substring(0, 1);
//checking if first char is already in the map
if (map.containsKey(firstChar)) {
//getting the count of that key
int count = map.get(firstChar);
// updating the key with adding 1
map.put(firstChar, count + 1);
} else {
// if this is first time than inserting with count as 1
map.put(firstChar, 1);
}
}
return map;
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME