I have the following case: I'm trying to loop over an ArrayList
and compare every element to an HashTable
. If the elements (string) are equal, I want to copy that in a new HashTable
neu . Until here it goes well. But I want, that if the word is already in the new HashTable
neu , to not put it again and simple add one count to the value which represents the frequency. I suppose that the else-block is never reached in this code, because test output it's never displayed. I know that until contentEquals
it works well, there it returns me all words which are equal.
So: how can I verify if an String is already in a HashTable
? I hope it's not duplicated, I have seen a lot of similar questions but didn't know how to use it correctly for my code.
Hashtable<String, Long> neu = new Hashtable<String, Long>();
ArrayList<String> words = new ArrayList<String>();
Hashtable<String, Long> count = new Hashtable<String, Long>();
// adding input
for (String s : count.keySet()) {
for (String x : words) {
if (x.contentEquals(s)) {
if (!neu.containsKey(s)) {
neu.put(s, (long) 1);
} else {
long p = neu.get(s);
p = p + 1;
neu.put(s, p);
System.out.println("test");
}
}
}
The Input is the following: For count:
try {
BufferedReader in = new BufferedReader(new FileReader(
"griechenland_test.txt"));
String str;
while ((str = in.readLine()) != null) {
str = str.toLowerCase(); // convert to lower case
String[] words = str.split("\\s+"); // split the line on
// whitespace, would return
// an array of words
for (String word : words) {
if (word.length() == 0) {
continue;
}
Long occurences = wordcount.get(word);
if (occurences == null) {
occurences = (long) 1;
} else {
occurences++;
}
count.put(word, occurences);
}
}
for words:
BufferedReader br = new BufferedReader(new FileReader("outagain.txt"));
for (String line = br.readLine(); line != null; line = br.readLine()) {
String h[] = line.split(" ");
words.add(h[0].toString());
}
Use the containsKey()
method to check if the given key is already in the hashtable.
static Hashtable<String, Long> map = new Hashtable<String, Long>();
public static void main(String[] args)
{
map.put("test", 1l);
System.out.println(map.containsKey("test")); // true
}
For string comparison, either use the equals(Object)
or equalsIgnoreCase(String)
method.
So your code should look like this...
for (String s : count.keySet())
{
for (String x : words)
{
if (x.equals(s))
neu.put(s, neu.containsKey(s) ? neu.get(s) + 1 : 1l);
}
}
Please verify the content of your words
and count
. This should be the issue, because your code is working fine.
I could manage it! I supposed the fault was really the way from input, there where no duplicateds, for that reason the "else" was never reached! I just eliminate that condition and changed a bit the structure. Thanks anyway all for your effort! :)
for (String x : hash.keySet()) {
long neu = hash.get(x);
for (String s : words) {
if (x.equals(s)) {
neuS.add(x);
neuZ.add(neu);
disc = disc + 1;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.