Java Dictionary Example
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
Java Dictionary Example的更多相关文章
- Java Dictionary 类存储键值
字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...
- Java Dictionary 类
Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似. 给出键和值,你就可以将值存储在Dictionary对象中.一旦该值被存储,就可以通过它的键来获取它.所以和Map一样, D ...
- (私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例)
(私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例) https://pan.baidu.com/s/1L54VuFwCdKVnQGVc8vD1TQnwmj java手册 Ja ...
- Java和Scala容器转换
参考:https://blog.csdn.net/dymkkj/article/details/77921573 Java和Scala互操作的一个重要的内容就是容器的转换,容器是一个语言的数据结构,表 ...
- Bean Validation规范
以下内容转载自:https://www.ibm.com/developerworks/cn/java/j-lo-beanvalid/ Bean Validation规范介绍 JSR303 规范(Bea ...
- scala成长之路(3)隐式转换
不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...
- 20180824-Java Enumeration 接口
Java Enumeration接口 Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素. 这种传统接口已被迭代器取代,虽然Enumeration 还未被 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- Java自定义一个字典类(Dictionary)
标准Java库只包含Dictionary的一个变种,名为:Hashtable.(散列表) Java的散列表具有与AssocArray相同的接口(因为两者都是从Dictionary继承来的).但有一个方 ...
随机推荐
- 入职日志——Solomon
心情 今天是所有培训结束后入职的第二天,紧张且期待. 紧张是因为昨天董经理有句话点醒了我. 你默默不再是个学生了.没有人会像你的老师父母一样担待你,原谅你. 职场是残酷无情的,是以结果为导向的.不论对 ...
- sql时间格式转换
sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...
- CentOS查看线程、硬盘、内存、cpu、网卡
1.查看硬盘 [mushme@investide ~]$ df -ah 2.查看内存 [mushme@investide ~]$ free -m 3.监控系统的负载 w 查看当前系统的负载,详细 ...
- isset() 与 array_key_exists() 比较
1.对于数组值的判断不同,对于值为null或''或false,isset返回false,array_key_exists返回true: 2. 执行效率不同,isset是内建运算符,array_key_ ...
- vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证)
vue+elementUI封装的时间插件(有起始时间不能大于结束时间的验证): html: <el-form-item label="活动时间" required> & ...
- 微信小程序获取手机信息
wx.getSystemInfo({ success: function (res) { console.log(res.model)//手机型号 console.log(res.pixelRatio ...
- vue入门学习示例
鄙人一直是用angular框架的,所以顺便比较了一下. <!DOCTYPE html> <html lang="en"> <head> < ...
- iOS7下Status Bar字体颜色修改
原文来自这里:iOS7下Status Bar字体颜色修改. 旧项目在iOS7上遇到status bar字体颜色需要修改的问题,症状如下:导航栏设置为黑色后,iphone上status bar的字体颜色 ...
- chromium之ref_counted
namespace subtle { class RefCountedBase { protected: RefCountedBase(); ~RefCountedBase(); void AddRe ...
- python初学者日记01(字符串操作方法)
时间:2018/12/16 作者:永远的码农(博客园) 环境: win10,pycharm2018,python3.7.1 1.1 基础操作(交互输入输出) input = input(" ...