Dictionary class is the abstract class which is parent of any class which uses the key and value pair relationship. The classes like HashTable extends this class for their functionality. Every key and every value is an object. In any one  Dictionary object, every key is associated with at most one value. As a rule, the  equals method should be used by implementations of this class to decide if two keys are the same. Also note that this class has become obsolete, the new implementation has to use the Map interface. Lets look at an example.

In the below example, I have created a “java.txt” file which the example code and reading the text file and printing it.

package javabeat.net.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; public class JavaDictionaryExample {
public static void main(String args[]) throws IOException,FileNotFoundException{
BufferedReader bufReader = new BufferedReader(new FileReader(new File(
"java.txt")));
String inputLine = null;
Map dictionaryMap = new HashMap();
while ((inputLine = bufReader.readLine()) != null) { // Here split the input line
String[] words = inputLine.split("\\s+");
if (inputLine.equals(""))
continue;
for (String wordStr : words) {
wordStr = wordStr.replace(".", "");
wordStr = wordStr.replace(",", "");
if (dictionaryMap.containsKey(wordStr)) {
Integer val = (Integer)dictionaryMap.get(wordStr);
dictionaryMap.put(wordStr, val + 1);
} else
dictionaryMap.put(wordStr, 1);
}
}
for (Object key : dictionaryMap.keySet())
System.out.println(key + ": " + dictionaryMap.get(key));
}
}

Output

1);: 2
: 28
Printing: 1
for: 2
dictionaryget(key));: 1
package: 1
javautilHashMap;: 1
readerclose();: 1
"inputtxt")));: 1
commas: 1
":: 1
main(String: 1
any: 1
empty: 1
import: 5
dots: 1
Hashtable();: 1
key: 1
": 1
else: 1
static: 1
wordreplace("": 2
Map: 2
+: 3
class: 1
inputLine: 1
javabeatnetcore;: 1
dictionarykeySet()): 1
and: 1
input: 1
reader: 1
javaioFileReader;: 1
FileReader(new: 1
args[]): 1
//: 5
String[]: 1
Systemoutprintln(key: 1
String: 1
:: 2
word: 3
lines: 1
Ignore: 1
javaioFile;: 1
=: 9
val: 2
javautilMap;: 1
javaioBufferedReader;: 1
inputLinesplit("\\s+");: 1
line: 1
dictionaryput(word: 2
File(: 1
HashMap();: 1
null): 1
words): 1
while: 1
JavaDictionaryExample: 1
words: 2
if: 2
map: 1
dictionaryget(word);: 1
Remove: 1
null;: 1
BufferedReader(new: 1
all: 1
readerreadLine()): 1
"");: 2
(dictionarycontainsKey(word)): 1
void: 1
continue;: 1
dictionary: 2
the: 2
stored: 1
in: 1
(String: 2
((inputLine: 1
new: 3
BufferedReader: 1
Split: 1
!=: 1
}: 5
(inputLineequals("")): 1
Integer: 1
public: 2
{: 5

Comments

最新文章

  1. 第五次团队作业——第一次项目冲刺——Alpha版本
  2. 设置Flush刷新模式setFlushMode()
  3. 探讨js字符串数组拼接的性能问题
  4. 十分钟搞懂什么是CGI
  5. 【转】变量的声明和定义,从C到编译原理到C++,再到Java
  6. DedeCms完美的FLASH幻灯代码
  7. 【转】发布的QT程序无法显示图标和图片的问题
  8. setjmp/longjmp 使用
  9. SecureCRT退出全屏方法
  10. smtp模块使用
  11. docker 保存 加载(导入 导出镜像
  12. Linux内存管理 (3)内核内存的布局图
  13. puppet 横向扩展(二)
  14. $Django importlib与dir知识,手写配置文件, 配置查找顺序 drf分页器&drf版本控制
  15. awk按顺序去除重复行
  16. 实验二 合作:王宏财 http://www.cnblogs.com/wanghongcai/
  17. Hive记录-配置远程连接(JAVA/beeline)
  18. Invitation Cards POJ-1511 (spfa)
  19. UINavigationController popToViewController用法
  20. 大文件上传插件webupload插件

热门文章

  1. Vue.js经典开源项目汇总
  2. 【转】Linux vmstat命令实战详解
  3. 在UML系统开发中有三个主要的模型
  4. 表中与当前库的collation不一致的字段
  5. Linux Shell 编程 文件转置问题
  6. 把list(对象)集合中的(某个属性),放到数组中。
  7. java soa接口测试,可以使用http协议调用
  8. SVN 操作报错 “Previous operation has not finished; run 'cleanup' if it was interrupted“
  9. Python书单
  10. 初窥UIKit Dynamics