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. was缓存以致web.xml更改无效

    was缓存导致web.xml更改无效 在项目中经常遇见这样的问题:修改应用的配置文件web.xml后,无论重启应用还是重启WebSphere服务器,都不能重新加载web.xml,导致修改的内容无效. ...

  2. HDU 5724 Chess(SG函数)

    Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  3. Centos7 yum安装mysql

    参考此文档:http://www.jb51.net/article/116032.htm http://www.jb51.net/article/95399.htm 1.在官网下载mysql57-co ...

  4. 【转】Android开发:shape和selector和layer-list的(详细说明)

    <shape>和<selector>在Android UI设计中经常用到.比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到<shape> ...

  5. Poj2919 Crane

    挑战程序设计竞赛的一道题 最近刚学了三角变换.于是就构造了个矩阵,没想到正是向量旋转的矩阵(不知道具体叫什么qwq 然后网上一半的题解是左闭右开的,另一部分是懒标记的. 于是便自己yy了一个左闭右闭的 ...

  6. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  7. OC中对象的description方法

    周所周知,我们在做项目时, 可以在类的.m文件中重写该类的对象的描述description方法: 示例: -(NSString *)description{    NSString *str = [N ...

  8. 使用Vue构建单页应用一

    一. 环境准备 1 安装Node.js  最好安装node.js-6.X.X 以上版本,https://nodejs.org/en/    我使用的是  v6.3.1 Current 版本.Node. ...

  9. String的非空判断、Integer的非空判断、list的大小判断,对象的非空判断

    1.String的非空判断. StringUtils.isNotEmpty(String str); 2.Integer的非空判断. null != Integer ; 3.list的大小判断. li ...

  10. Knowledge Point 20180303 详解main函数

    学习Java的朋友想来都是从HelloWorld学起的,那么想来都对main函数不陌生了,但是main函数究竟是怎么回事呢?main函数中的参数是做什么的呢?main函数为什么能作为程序的入口呢?可不 ...