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继承来的).但有一个方 ...
随机推荐
- jemter 使用if控制器,选择需要的内容
背景:需要根据人员传入的变量,来选择运行的环境,调用不同的参数,进行拼接,使用到if控制器 取到的数据,调用的就是test1的数据
- jquery获取前一个月日期
一) 重构Date对象: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年 ...
- UVA12034 Race
嘟嘟嘟 令dp[i]表示在n个人中,有 i 个人获得第一名的方案数,转移方程为dp[i] = C(i, n) * dp[n - i].C(i, n)就是从n个人中选 i 个第一,那么剩下的n - i ...
- 大数据-图表插件-echarts 样式修改(迭代)
修改折线图大小 myChart.setOption({ title:{ text:"价格指数" ...
- 在win7中通过手机投放媒体
依次展开>>> 设置项 开启服务项: 和 在更给网络属性为 打开wmplayer开启两个允许 在手机端无线投屏选择设备即可
- windows自定义快捷键功能
如下:
- MS12-020蓝屏攻击
MS12-020远程桌面协议RDP拒绝访问漏洞 条件:受害者必须开放RDP协议 开放了3389端口 或者端口改了,知道对方RDP开放的对应端口. 过程:MSF利用 MSF显示为seems down说明 ...
- linux下构建SVN
1. 安装subversion#yum -y install subversion2. 安装好了之后 新建一个svn目录#mkdir /home/svn3. 新建两个版本仓库#svnadmin cre ...
- iOS之序列化与反序列化
所谓的序列化和反序列化就是将数据结构或对象和二进制串之间相互转换的过程: 本人的理解是当你于写数据需要本地存储时,即将你的数据写到硬盘上的时候,你就必须对他进行序列化,转换成二进制文件,从而便于在磁盘 ...
- SharePoint2013之双语切换
最近遇到个项目需要用SharePoint2013实现双语.看了篇老外的博客,经过项目经理的指点,终于算是搞定了(后面解释为什么说是"算是"). 在SharePoint2013中不像 ...