Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    public static Map<String, Integer> countOccurencesOfWords(String str) {
        String[] words = str.split(("[\\p{IsPunctuation}\\p{IsWhite_Space}]+"));
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (String s : words) {
            System.out.println(s);
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }
        return map;
    }
}