Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package javaapplication66;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- /**
- *
- * @author Usuario
- */
- public class JavaApplication66 {
- public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
- {
- // Create a list from elements of HashMap
- List<Map.Entry<String, Integer> > list =
- new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
- // Sort the list
- Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
- public int compare(Map.Entry<String, Integer> o1,
- Map.Entry<String, Integer> o2)
- {
- return (o2.getValue()).compareTo(o1.getValue());
- }
- });
- // put data from sorted list to hashmap
- HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
- for (Map.Entry<String, Integer> aa : list) {
- temp.put(aa.getKey(), aa.getValue());
- }
- return temp;
- }
- public String mostCommonWord(String paragraph, String[] banned) {
- //
- String[] arr = paragraph.split(" ");
- for(int i =0; i<arr.length; i++) {
- arr[i] = arr[i].toLowerCase().trim();
- char ultimo = arr[i].charAt(arr[i].length() - 1);
- //if( ultimo == ',' || ultimo == '.' || ultimo){
- if(!Character.isLetter(ultimo)){
- arr[i] = arr[i].substring(0, arr[i].length() - 1);
- }
- }
- HashMap<String, Integer> hash_parag =
- new HashMap();
- for(int i =0; i<arr.length; i++) {
- if(hash_parag.containsKey(arr[i])) {
- hash_parag.put(arr[i], hash_parag.get(arr[i]) + 1);
- }
- else
- {
- hash_parag.put(arr[i], 1);
- }
- }
- HashMap<String, Integer> ord_hash_parag = sortByValue( hash_parag );
- HashSet<String> hash_banned =
- new HashSet();
- for(int i =0; i<banned.length; i++) {
- hash_banned.add(banned[i]);
- }
- for(String key: ord_hash_parag.keySet()) {
- if(!hash_banned.contains(key)) {
- return key;
- }
- }
- return "";
- }
- public static void main(String[] args) {
- // TODO code application logic here
- String paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
- String[] banned = {"hit"};
- String[] arr = paragraph.split(" ");
- for(int i =0; i<arr.length; i++) {
- arr[i] = arr[i].toLowerCase().trim();
- char ultimo = arr[i].charAt(arr[i].length() - 1);
- if( ultimo == ',' || ultimo == '.'){
- arr[i] = arr[i].substring(0, arr[i].length() - 1);
- }
- }
- HashMap<String, Integer> dic =
- new HashMap();
- for(int i =0; i<arr.length; i++) {
- if(dic.containsKey(arr[i])) {
- dic.put(arr[i], dic.get(arr[i]) + 1);
- }
- else{
- dic.put(arr[i], 1);
- }
- }
- HashMap<String, Integer> hash = sortByValue(dic);
- for(String key: hash.keySet()) {
- System.out.println(key + " " + hash.get(key) );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement