Program To Find A Number of Words In String Using HashMap
import java.util.HashMap;
public class FinalCountWords {
public static void main(String[] args) {
String str = "I am Developer java java";
String[] split = str.split(" ");
HashMap<String,Integer> map = new HashMap<String,Integer>();
for (int i=0; i<split.length-1; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count+1);
}
else
{
map.put(split[i], 1);
}
}
System.out.println(map);
}
}
Output:
{I=1, am=1, Developer=1, Java=2}
0 comments 10:
Post a Comment