-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazonHashMap.java
More file actions
84 lines (75 loc) · 3.06 KB
/
Copy pathAmazonHashMap.java
File metadata and controls
84 lines (75 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* 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 examples;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
*
* @author
*/
public class AmazonHashMap {
public static void example(){
int[] entry = {1,2,2,3,3,4,5,5,6,6,7,8,9,9,10,10};
evaluate(entry);
}
public static void stringText(){
String currentText = "A 401k is a retirement savings plan sponsored by an employer . It lets workers save and invest a piece of their paycheck before taxes are taken out . Taxes are not paid until the money is withdrawn from the account . 401k plans , named for the section of the tax code that governs them , arose during the 1980s as a supplement to pensions . Most employers used to offer pension funds .";
evaluate(currentText);
}
public static void evaluate(String text){
String[] words = text.split(" ");
List<String> excludeWords = new ArrayList<>();
HashMap<String, Integer> hashMapFrequency = new HashMap<>();
int maxFrequency = 1;
excludeWords.add("a");
excludeWords.add("by");
excludeWords.add("an");
excludeWords.add("it");
excludeWords.add(".");
excludeWords.add(",");
excludeWords.add("of");
excludeWords.add("are");
excludeWords.add("not");
excludeWords.add("for");
excludeWords.add("the");
excludeWords.add("as");
excludeWords.add("is");
excludeWords.add("to");
for(String currentWord : words){
if(excludeWords.contains(currentWord.trim()) || currentWord.trim().length() == 0){ continue; }
String auxWord = currentWord.trim().toLowerCase();
if(hashMapFrequency.get(auxWord) == null){
hashMapFrequency.put(auxWord, 1);
}else{
int frequency = hashMapFrequency.get(auxWord) + 1;
maxFrequency = frequency;
hashMapFrequency.put(auxWord, frequency);
}
}
for(String keyWord : hashMapFrequency.keySet()){
if(hashMapFrequency.get(keyWord) == maxFrequency){
System.out.println(keyWord + " " + maxFrequency);
}
}
}
public static void evaluate(int[] arrayToEvaluate){
HashMap<Integer, Integer> hashMapFrequency = new HashMap<>();
for(int k=0; k < arrayToEvaluate.length; k++){
if(hashMapFrequency.get(arrayToEvaluate[k]) != null){
int frequency = hashMapFrequency.get(arrayToEvaluate[k]) + 1;
hashMapFrequency.put(arrayToEvaluate[k], frequency);
}else{
hashMapFrequency.put(arrayToEvaluate[k], 1);
}
}
for(int k=0; k < arrayToEvaluate.length; k++){
if(hashMapFrequency.get(arrayToEvaluate[k]) == 1){
System.out.println(arrayToEvaluate[k]);
}
}
}
}