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的更多相关文章

  1. Java Dictionary 类存储键值

    字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...

  2. Java Dictionary 类

    Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似. 给出键和值,你就可以将值存储在Dictionary对象中.一旦该值被存储,就可以通过它的键来获取它.所以和Map一样, D ...

  3. (私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例)

    (私人收藏)[开发必备]最全Java离线快速查找手册(可查询可学习,带实例) https://pan.baidu.com/s/1L54VuFwCdKVnQGVc8vD1TQnwmj java手册 Ja ...

  4. Java和Scala容器转换

    参考:https://blog.csdn.net/dymkkj/article/details/77921573 Java和Scala互操作的一个重要的内容就是容器的转换,容器是一个语言的数据结构,表 ...

  5. Bean Validation规范

    以下内容转载自:https://www.ibm.com/developerworks/cn/java/j-lo-beanvalid/ Bean Validation规范介绍 JSR303 规范(Bea ...

  6. scala成长之路(3)隐式转换

    不废话,先上例子:定义一个参数类型为String的函数: scala> def sayHello(name:String) = println("hello " + name ...

  7. 20180824-Java Enumeration 接口

    Java Enumeration接口 Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素. 这种传统接口已被迭代器取代,虽然Enumeration 还未被 ...

  8. Spark案例分析

    一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...

  9. Java自定义一个字典类(Dictionary)

    标准Java库只包含Dictionary的一个变种,名为:Hashtable.(散列表) Java的散列表具有与AssocArray相同的接口(因为两者都是从Dictionary继承来的).但有一个方 ...

随机推荐

  1. IBM MQ介绍

    转自:http://hi.baidu.com/lubezhang/blog/item/bd308b3b7ecce3ec14cecb4f.html IBM MQ(IBM Message Queue)是I ...

  2. Tomcat组件启动流程图

    看到一张关于Tomcat组件启动流程图,觉得还可以,收藏.

  3. Oracle中的rownum不能使用大于>的问题

    标题:Oracle中的rownum不能使用大于>的问题 一.对rownum的说明 关于Oracle 的 rownum 问题,很多资料都说不支持SQL语句中的“>.>=.=.betwe ...

  4. c#根据当前时间获取本周,本月,本年度等时间段和DateTime日期格式化

    DateTime dt = DateTime.Now; //当前时间 DateTime startWeek = dt.AddDays( - Convert.ToInt32(dt.DayOfWeek.T ...

  5. BZOJ 1878 [SDOI2009]HH的项链 【莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=1878 1878: [SDOI2009]HH的项链 Time Limit: 4 Sec  M ...

  6. spring注入bean的三种方法

    在Spring的世界中, 我们通常会利用bean config file 或者 annotation注解方式来配置bean. 在第一种利用bean config file(spring xml)方式中 ...

  7. VC MFC工具栏(CToolBar)控件(转)

    工具栏 工具栏控件在控件面板里没有对应的选项(图标),但有一个工具栏控件类CToolBar,所以我们如果要创建一个工具栏控件并显示在窗口里的话,只能用代码来完成,事实上任何一种控件,都可以用代码创建, ...

  8. Oracle的分析函数

    Oracle的分析函数row_number(),rank(),dense_rank()的用法与区别 比如查询工资排名第7的员工信息,可以用分析函数来做. --查询工资排名第7的员工信息select * ...

  9. ajax 与 axios区别

    Ajax: Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发技术. Ajax = 异步 J ...

  10. 持续集成(CI – Continuous Integration)

    持续集成(CI – Continuous Integration) 在传统的软件开发中,整合过程通常在每个人完成工作之后.在项目结束阶段进行.整合过程通常需要数周乃至数月的时间,可能会非常痛苦.持续集 ...